Let us say, I want to return a name:
$re = "/(name is )[a-z- ]*( )/i";
$str = "My name is John ";
preg_match($re, $str, $matches);
print_r($matches);
The result is:
Array
(
[0] => name is John
[1] => name is
[2] =>
)
Well, let's see this:
$string = "My name is John and I like Ice-cream";
$pattern = "My name is $1 and I like $2";
With this I get: Array
(
[0] => name is John and I like Ice-cream
[1] => name is
[2] => and I like
[3] =>
)
which is more or less the same string I passed.
What I am looking for is to compare and extract the variables so that I could use them as variable $1
and $2
or anything like this works.
Or maybe a method that returns an associate array or those items like:
Array("1" => "John" , "2" => "Ice-cream")
Any workaround or ideas are highly appreciated as long as they work in the way I asked above.