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</bBefore the <a
href='http://example.com'>Ex ample</a><br>\n- "
."<a href='http://example.com/example.php?id= {{111}}'>Exampl e
1</a><br>\n- Now for 2:"
."<a href=\"http://example.com/example.php?id= {{2222}}\">Exam ple
2</a><br>\n- Now for 3:"
."<a href='http://example.com/example.php?id= {{33}}&m=1'>Exa mple
3</a><br>\n- Now for 4:"
."<a href=\"http://example.com/example.php?id= {{111}}\">Examp le
4</a<br>\n- "
."Some <b>Stuff</bAfter the <a
href='http://example.com'>Ex ample</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($matche s 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><texta rea cols=100 rows=4>$pattern \n";
preg_match($pat tern, $html, $ms);
print_r($ms);
echo "</textarea>";
#make the replacements:
$html=preg_repl ace($pattern,$r eplace,$html);
}
#show the new html
echo "<br><texta rea 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.
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</bBefore the <a
href='http://example.com'>Ex ample</a><br>\n- "
."<a href='http://example.com/example.php?id= {{111}}'>Exampl e
1</a><br>\n- Now for 2:"
."<a href=\"http://example.com/example.php?id= {{2222}}\">Exam ple
2</a><br>\n- Now for 3:"
."<a href='http://example.com/example.php?id= {{33}}&m=1'>Exa mple
3</a><br>\n- Now for 4:"
."<a href=\"http://example.com/example.php?id= {{111}}\">Examp le
4</a<br>\n- "
."Some <b>Stuff</bAfter the <a
href='http://example.com'>Ex ample</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($matche s 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><texta rea cols=100 rows=4>$pattern \n";
preg_match($pat tern, $html, $ms);
print_r($ms);
echo "</textarea>";
#make the replacements:
$html=preg_repl ace($pattern,$r eplace,$html);
}
#show the new html
echo "<br><texta rea 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.
Comment