Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

preg_replace_all problem

Posted on 2004-11-02
3
Medium Priority
?
3,153 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:mag1c1an
  • 2
3 Comments
 
LVL 48

Accepted Solution

by:
hernst42 earned 2000 total points
ID: 12478333
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
 
LVL 2

Author Comment

by:mag1c1an
ID: 12478496
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
 
LVL 2

Author Comment

by:mag1c1an
ID: 12478541
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month21 days, 7 hours left to enroll

804 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