Regular expression / preg_match and preg_match_all

Hi, I have a regular expression that I want to use to replace in a text but I haven´t figured out how to.
Using preg_replace I mange to replace it with some random text but I need to first work with the match and based on it the replacement text changes, I read the php.net manual and used preg_match and preg_match_all trying to store the matches in a variable but when I try to use the variable is null, can someone give me an idea

My regular expression


$regex ="/\\{\\|plugin\\|.{6}\\|[a-z]{2}_[A-Z]{2}\\|\\d{3}\\|\\}/";

this is how I’m changing the text


preg_replace($regex, "changed_text", $text);//this is working so my regular expression does work

this is the target text, in red the text that never changes, in green the text that is case sensitive

{|plugin|fbcomm|fr_FR|350|}

and this is how I am trying to store the matches to further work with them, it is either not working for me or have not really understood how to use the variable where the matches are stored, I tried both match and match_all


preg_match_all($regex, $text, $matches);
preg_match($regex, $text, $matches);

I tried using them like this


function change_text($matches[0]){
//do something to $matches[0]
}
//I also tried
function change_text($matches[0][0]){
//do something to $matches[0][0]
}

but both return NULL

You can test your regex here: http://www.solmetra.com/scripts/regex/index.php
It’ll show you what will be returned in $matches, so you can see if it is what you want/expect.

well, as I mentioned I am sure the regular expression works I tested it and I am able to replace it with some other text, what I am not able to do is use $matches to work with them and then replace it

And as I mentioned using the page I linked to you can easily see what is returned by preg_match and preg_match_all. Of course, you can do the same adding some var_dump statements in your code.

As far as your “use $matches” is concerned, who know why it doesn’t work. You didn’t show us what you are trying to do with it.

Right after,

preg_match_all($regex, $text, $matches);

do a print_r($matches);

then to call your function,

change_text($matches[0]);

Your function should look like,
function change_text($matched){
echo $matched;
}

The page that you provided me does work, thank you and I will try to provide the necessary information on my next post, however I did mention that my regular expression works, I don’t know what was the reason $matches was not working but after waking up this morning and turning the computer back on the script worked just fine