preg_replace_all problem

Posted on 2004-11-02
Last Modified: 2012-05-05
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
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..

$content = join("",file("test.tpl"));  // check template.tpl path

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

foreach ($matches as $key =>$value)
      $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 ="";
echo "<br>".$content;


*** end code ***

Question by:mag1c1an
    LVL 48

    Accepted Solution

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

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

    foreach ($matches as $key =>$value) {
        foreach($value[0] as $i => $v) {
            if (empty($v)) {
            $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;

    LVL 2

    Author Comment

    Hi Hernst42,
    Its not working.....can you look at this question as the whole script is written there:

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

    LVL 2

    Author Comment

    This is my template file:

    And here are some cartoons:<br>
    {2 3}
    Or if  you prefer full movies:
    <br><b>{1 15} </b>
    Third and final one:
    <br><i>{3 3} </i>
    <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}

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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now