|
Posted by ctiggerf on 08/11/06 17:08
I am having a regular expression problem with a bit of code I'm working
on. I was able to get a similar piece of code working in perl, but now
I'm trying to convert it over. Basically what I'm trying to do here is
some thing like this
Get some html from an external file
Look for my ids inside of the html, my ids look like {{###}}.
Replace all the urls the ids are in with a url I get from my database
matching the id.
Here is a little sample code:
<?php
#some html - normally pulled from a text file on a server
$html="Some <b>Stuff</b> Before the <a
href='http://example.com'>Example</a><br>\n- "
."<a href='http://example.com/example.php?id={{111}}'>Example
1</a><br>\n- Now for 2:"
."<a href=\"http://example.com/example.php?id={{2222}}\">Example
2</a><br>\n- Now for 3:"
."<a href='http://example.com/example.php?id={{33}}&m=1'>Example
3</a><br>\n- Now for 4:"
."<a href=\"http://example.com/example.php?id={{111}}\">Example
4</a<br>\n- "
."Some <b>Stuff</b> After the <a
href='http://example.com'>Example</a><br>";
#Disply the Orignal
echo "<textarea cols=100 rows=6>$html</textarea>";
#Find all the id's
preg_match_all("/{{(\d+)}}/", $html, $matches, PREG_SET_ORDER);
foreach($matches as $match) {
$id=$match[1];
#the replace string - this would normally be pulled from a database
matching the id
$replace="http://example2.com/example2.php?id=$id";
#the pattern
$pattern="/href=('|\")[^\1]*?{{".$id."}}[^\1]*?\1/i";
#print out what matches:
echo "<br><textarea cols=100 rows=4>$pattern\n";
preg_match($pattern, $html, $ms);
print_r($ms);
echo "</textarea>";
#make the replacements:
$html=preg_replace($pattern,$replace,$html);
}
#show the new html
echo "<br><textarea cols=100 rows=6>$html</textarea>";
?>
I've tried just about everything I can think of in that second regular
expression and can't get it to match. Any help you can give would be
appreciated.
Thank you,
Chris.
[Back to original message]
|