I'm writing a php file that will search through a web page's source code, replacing a user-given string with another user-given string. The problem is, if a user were to replace all occurences of "href" (or "a" for that matter) it would alter the HTML code and, in this case, all hyperlinks would cease to function. I'm trying to find a way to replace only the occurences of the string that are not within HTML tags. I've tried using ereg_replace by making a regular expression something like this:
ereg_replace("(>[<]*)".$stringtoreplace, "\1".$stringreplacewith, $string);
As you can see it looks for a closing tag followed by 0 or more non-opening tags and the string, then replaces with the original stuff and the new string.
This kind of works, but for some reason it will only replace one occurence of the string between > and <. Anyway, I figured there must be a better way to do this. What would be nice is a "is_string_in_tags()" function in PHP, but the closest I've found is strip_tags() and I can't see how that would help me as I want to keep the HTML in the string. Any ideas?