preg_replace_all problem

Hi,
I am basically opening a .tpl file and replaceing the tag's which look like this:
{movies 30}
in the above example "movies" is key and 30 is the value

the problem that I am having is if I have 2 tags that are movies
eg:
30 movies: {movies 30}
more movies: {movies 60}

I am getting a double dim array instead of a single one...

heres the code i am using..

<?php
$content = join("",file("test.tpl"));  // check template.tpl path
$categories=array("1","2","3","textlinks");

foreach ($categories as $value)
            {
        $pattern = "/{".$value." ([0-9_]+)?}/";
         preg_match_all($pattern,$content,$matches[]);
            }

$index=0;
foreach ($matches as $key =>$value)
      {
      preg_match_all("/\w+/",$value[0][0],$res);
      $q = "select client from t_members where category_number='".$res[0][0]."' limit ".$value[1][0]."\n";

      echo $q."<br><br>";

       $content = str_replace($matches[$index][0],$value,$content);
      $value ="";
       $index++;
       }
echo "<br>".$content;

?>

*** end code ***


Thanks,
Mag
LVL 2
mag1c1anAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hernst42Commented:
preg_match_all returns a multidimensional array, so you also have to loop over that array like this:


$categories=array("1","b","a","textlinks");
foreach ($categories as $value) {
    $pattern = "/{".$value." ([0-9_]+)?}/";
    preg_match_all($pattern,$content,$matches[]);
}

$index=0;
foreach ($matches as $key =>$value) {
    foreach($value[0] as $i => $v) {
        if (empty($v)) {
            continue;
        }
        preg_match_all("/(\w+)/",$v,$res);
        $q = "select client from t_members where category_number='".$res[1][0]."' limit ".str_replace('_', ',', $res[1][1])."\n";
        $content = str_replace($value[0][$i],$q,$content);
    }
}
echo "<br>".$content;

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mag1c1anAuthor Commented:
Hi Hernst42,
Its not working.....can you look at this question as the whole script is written there:
http://www.experts-exchange.com/Web/Web_Languages/PHP/Q_21190741.html

and this question and that one is related....if you can solve it i'll give you the points for both of then...

Thanks,
MAg
0
mag1c1anAuthor Commented:
This is my template file:


<html>
<body>
And here are some cartoons:<br>
{2 3}
<br><br>
Or if  you prefer full movies:
<br><b>{1 15} </b>
<br><br>
Third and final one:
<br><i>{3 3} </i>
<br><br><br>
<b>And here come the links 1-15</b>
<br>{textlinks 0_15}

<b>And here come the links 15-30</b>
<br>{textlinks 16_30}
</body>
</html>


and when i say its not working is because its ignoring
{2 3}  and {3 3}


Thanks,
Mag
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.