0

I have a list of regular expressions like

$regex = "{Hello ([a-zA-Z]+), you are ([0-9]{2}) years old today\.}u"

Is there a function to do something like the following :

$result = function_i_am_looking($regex, "John", 25);
echo $result; // Outputs : "Hello John, you are 25 years old today."

Note: this is not some kind of templating engine I'm building ;)

Note 2: I can't predict what regex will be and in what order.

9
  • preg_replace is the command, doc linked Your $regex isn't valid PRCE Regex though. Commented Aug 7, 2014 at 12:01
  • 1
    The closest thing is sprintf for writing strings from a format, but that's not the same thing as a regex Commented Aug 7, 2014 at 12:02
  • @t3chguy Where is it wrong ? Commented Aug 7, 2014 at 12:03
  • @hek2mgl : I was thinking about replacing each parenthesis with the paramters given, in specific order. Commented Aug 7, 2014 at 12:03
  • 1
    @t3chguy {} are allowed for regex in PHP (in fact, any non alphanumerical characters ;) Commented Aug 7, 2014 at 12:11

4 Answers 4

1

Having the restriction that matching groups aren't nested in the regex patterns, you can use this:

$regex = "/Hello ([a-z]+), you are ([0-9]{2}) years old today./u";

$replacements=array("John", 25);
$result = preg_replace_callback('/\((.*?)\)/', function($m) use (&$replacements) {
        return array_shift($replacements);
}, $regex);

echo $result; // Outputs : "Hello John, you are 25 years old today."

IMO this is already the best - generic - thing you can do. However, while it works (a bit :) ), I don't think it is a good idea to do so. What you want is a template engine, or even printf (yes, you want that ;) )

Sign up to request clarification or add additional context in comments.

5 Comments

printf, like sprintf are interesting, but I cannot make them work the other way around (matching something like "Hello hek2mgl, you are 37 years old today.") ... except if I change the original preg_replace by replacing "%s" by "(\w+)", etc
Like the anonymous function too ;)
@CyrilN. Don't expect too much from that solution. I can only say that you are not the first person searching for a way to use a regex as a) parser b) format specifier. I think to remember a project written in C. let me try to find it again for you ...
@CyrilN. Check this blog.dotnetwiki.org/… ... It is not exactly the same as you want, but if you need you might settle on this or at least their ideas. There are also related projects, stress your google. Good luck!
Nice, this is interesting! Thanks :)
1

You maybe want to use sprintf()

sprintf("Hello %s, you are %d years old today.", "John", 25);

would print:

Hello John, you are 25 years old today.

3 Comments

Thanks for the suggestion but I already have the regex in use, I can't change it to a sprintf accepted format :/
EXCEPT if you can make the other way around work, suppose you have multiple sentences in an array, including the one in the example, and I give you the text "Hello Wumm, you are 14 years old today.", will you be able to match it to "Hello %s, you are %i years old today." ?
@CyrilN. No, I don't see any way to do this. (using sprintf format specifiers)
0

you can use some mixed substring for replacing them

$your_string = "Hello @##@YOUR_NAME@##@, you are @##@YOUR_AGE@##@ years old today.";

$new_string = get_full_string($your_string, "John", 25);

echo $new_string;

function get_full_string($str, $name, $age)
{
     $str = str_replace("@##@YOUR_NAME@##@", $name, $str);
     $str = str_replace("@##@YOUR_AGE@##@", $age, $str);
     return $str;
}

LIVE DEMO

METHOD : 2

$your_string = "Hello ([a-z]+), you are ([0-9]{2}) years old today.";

$new_string = get_full_string($your_string, "John", 25);

echo $new_string;

function get_full_string($str, $name, $age)
{
     $str = str_replace("([a-z]+)", $name, $str);
     $str = str_replace("([0-9]{2})", $age, $str);
     return $str;
}

Demo

2 Comments

Thanks but the regex system is already in place, I can't change it, except if you can make it work both ways (take a look at the comments I made to @wumm answer)
Thanks for your suggestion. I may have missed a point : I can't predict the regex structure (which parameters and in what order).
0

How about:

$regex = "{Hello ([a-z]+), you are ([0-9]{2}) years old today.}u";
$result = function_i_am_looking($regex, "John", 25);
echo $result; 

function function_i_am_looking($reg, $str, $num) {
    $reg = preg_replace('/\(\[a-z\].*?\)/', '%s', $reg);
    $reg = preg_replace('/\(\[0-9\].*?\)/', '%d', $reg);
    return sprintf($reg, $str, $num);
}

1 Comment

Thanks for your suggestion. I may have missed a point : I can't predict the regex structure (which parameters and in what order).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.