Jump to content

[SOLVED] Find and replace text in preg_match array


WEvans

Recommended Posts

I'm trying to change a font color html code once my preg_match_all reads the file. Now the code only shows up when a winner has been posted for that tour. Otherwise it's not visible. So I want to search the preg results which is $matches, for the "font color=3FFF5E" and replace it with "font color=FFFFFF". If the code is not found, continue what it was doing. Here's what I THOUGHT might work. lol

 

<?php 
$html = file_get_contents(
'http://www4.igl.net/cgi-bin/tourney/gencalendar.cgi?path=crazyhouse');
preg_match_all('|"#0000AA">(.*?)<font size=1 color=yellow face=Arial>(.*?)</a><br><font size=1 color=white face=Arial>(.*?)</td>|is',$html,$matches,PREG_SET_ORDER);
$pattern = 'color=3FFF5E';
$replace = 'color=FFFFFF';


echo "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"175px\">";
foreach ($matches as $match){
preg_replace($pattern, $replace, $match[0]);
echo "<tr><td class=\"caltable\">$match[1]<font color=#FFFFFF class=\"callinkfnt\">$match[2]</font></a><br>$match[3]</tr></td>";
}
echo "</table>";

?>

 

I tried to seperate that code with preg_match_all and change the color there, as I did with the other font code you see. The problem is, it won't show any of the entries, if that code doesn't show up to begin with. Bad results. I do know that the change will take place in $match[3], if that helps at all. I tried to change 0 to 3 but didn't work.

Thanks for the help in advance.

I fixed it. Instead of messing with preg, i used str_replace on the file_get_contents. Changed it before it got chopped up in preg_match.

<?php 
$pattern = "color=3FFF5E";
$replace = "color=FFFFFF";
$html = file_get_contents(
'http://www4.igl.net/cgi-bin/tourney/gencalendar.cgi?path=crazyhouse');
$html = str_replace($pattern, $replace, $html);
preg_match_all('|"#0000AA">(.*?)<font size=1 color=yellow face=Arial>(.*?)</a><br><font size=1 color=white face=Arial>(.*?)</td>|is',$html,$matches,PREG_SET_ORDER);

echo "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"175px\">";
foreach ($matches as $match){
echo "<tr><td class=\"caltable\">$match[1]<font color=#FFFFFF class=\"callinkfnt\">$match[2]</font></a><br>$match[3]</tr></td>";
}
echo "</table>";

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.