Forum Moderators: coopster
found this thread which is probably the opposite regex to what I need.
Will try to understand how it works..
Yep, that thread looks like it has a very addaptable example for your purpose. One thing to be aware of is that ? is a special character in regexs, so to use it literally, you'll need to escape it with a backslash: \?
<edit> Ooops, I was thinking you were replacing '?' when I wrote that, but it was '&' -- which you don't have to worry about.
After studying this [devplanner.com...]
I came up w/ the following, but it is not matching a single thing ... :(
$text = '<a href="http://www.site.com/index.php?param=123¶m2=345"> stuff';
$lookfor = "=";
$pattern = '#(<.*?)(\\B'.$lookfor.'\\B)([<>]*?>)#';
$replacement = '_eq_';
$text = preg_replace($pattern, $replacement, $text );
echo $text;
<?php
$text = "<a
href=
\"http://www.site.com/index.php?param=123¶m2=345¶m3=678\">
<span style=\"color:red;\"> this=stuff, </span></a>
& this=more stuff, <a href=\"http://www.site.com/index.php?param=123¶m2=345\">
&this=final stuff</a>";
echo htmlspecialchars($text)."<br> <br>\n\n"; //just to see with browser
$look_fors = array('=', '&');
$replace_withs = array('_eq_', '_AND_');
for($i = 0; $i < count($look_fors); $i++) {
$pattern = "#(<a[^>]*?)$look_fors[$i](.*?>)#s";
$replacement = "\${1}$replace_withs[$i]\$2";
while (preg_match($pattern,$text)) {
$text = preg_replace($pattern, $replacement, $text );
}
}
echo "$text<br> <br>\n\n";
echo htmlspecialchars($text)."<br> <br>\n";
?> The '<a' in $pattern will cause the replacements to be made only in anchor tags.
If you want them to be made in all tags, simply remove the 'a'. If you don't
what to replace the '=' in '<a href=', simply include all of that in $pattern,
etc.
I hope this helps.