Hi,
I totally suck at RegEx (but am trying to learn), I got the following from
the web, but its not working for me...
can anyone spot what I am doing wrong or whats wrong please?
Its a small script and just around 8 lines are actually important (I think).
Its basically something like a "template" program, it searches for the
'tags' and replaces them, but in this case I also want the numbe of each of
the tags as thats how many records have to be returned for that category...
eg:
<!--cartoons 25-->
means that I query the database for 25 records for category "cartoons" and
replace that whole 'tag' with the result of the query...
///// start code /////
<?php
$fileContents=<<<blah
<html>
<body>
And here are some cartoons:
<!--cartoons 25-->
<br><br>
Or if you prefer full movies:
<!--movies 15-->
</body>
</html>
blah;
$found = array();
if (preg_match_all('#<!--\s*([^>\s])\s+(\d+)\s*-->#', $fileContents,
$matches, PREG_SET_ORDER)) {echo "in preg_match";
foreach ($matches as $match) {
$found[$match[0]] = $match[1];
echo "in first foreach";
}
}
//Get the data:
$data = array();
foreach ($found as $key => $count) {
$data[$key] = "\$key was: $key and \$count was: $count";/* YOUR data as
HTML string for $key with $count rows */
echo "In second foreach() ".$key. " ".$count;
}
//Replace the "tags":
// create RegEx tags
$regExTags = array();
foreach ($found as $key => $count) {
$regExTags[] = sprintf(
'#<!--\s*%s\s+%d\s*-->#',
preg_quote($key, '#'),
$count
);
}
$fileContents = preg_replace($regExTags, $data, $fileContents);
print_r($fileContents);
?>
///// end code /////
Thanks,
Ryan
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php