Forum Moderators: coopster
Can someone write the general expression I need to use? I've been trying to get it right to no avail.
Search For...
<DT><A HREF="/variable_directory_name/dir/pagename.shtml" ADD_DATE="1043532847" LAST_VISIT="1043532847" LAST_MODIFIED="0">
Replace With...
<DT><A HREF="/variable_directory_name/dir/pagename.shtml" ADD_DATE="1043532847" LAST_VISIT="1043532847" LAST_MODIFIED="0"> <img src="/gr/gs/graphicname.gif">
$html_file = // html file contents
$image_tag = "<img src=\"/gr/gs/graphicname.gif\">";
$regex_pattern = "#(<DT><A HREF=\"/.*LAST_MODIFIED=\"0\">?)(?!$image_tag)#i";
while (preg_match($regex_pattern,$html_file,$matches) {
$html_file = preg_replace($matches[1],$matches[1].$image_tag,$html_file);
}
This isn't tested, but I think it might work out of the box. At least the way it's meant to work is that the stuff in the first set of () of the pattern will be stored in $matches[1] and should match all the variations that you want. The .* will match any number of any characters between the literal strings on either side.
The second set of () is an assertion that will disallow any matches that already
have the image tag after them. That part of the pattern will allow you to make
the replacements in a while loop that will run until all of the replacements are
done.
At the end of the pattern, the i after the # boundary is to make the search
case-insensitive. If you are certain about the cases of the tags, you can omit
it if you want.
As I said, this is untested, so if you have any problems that you have trouble
solving, just post what error messages you get.
I hope this helps.
It's a bit crude, but at least it worked with a test $html_file requiring four iterations. while (preg_match('@(<DT><A HREF="/.*LAST_MODIFIED="0">)(?!<img src="/gr/gs/graphicname.gif">)@i',$html_file,$matches))
{
$pattern = "#".$matches[1]."#";
$replacement = $matches[1]."<img src=\"/gr/gs/graphicname.gif\">";
$html_file = preg_replace($pattern,$replacement,$html_file);
}
I wish you well.
[edited by: coopster at 12:22 am (utc) on Jan. 4, 2005]
[edit reason] Disabled graphic smile faces for this post [/edit]