RE: [PHP] Regex question: replacing incidences of character when not enclosed within HTML tags? (somewhat solved)
| From: | Murray @ PlanetThoughtful | Date: | Mon, 30 May 2005 02:33:27 +0000 |
| Subject: | RE: [PHP] Regex question: replacing incidences of character when not enclosed within HTML tags? (somewhat solved) | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-215976@lists.php.net to get a copy of this message | ||
> So, thinking about it a little more, I decided what I was looking for was
> a
> regular expression that would allow me to replace any incidences of
> hyphens
> when not contained within tags (i.e., when not contained between "<" and
> ">").
>
> And this is where things have ground to a halt.
Hi All,
After toying with this for several hours I decided to give up on trying to
work out a way to achieve this via a single regular expression replacement.
My simpler 'solution' now is to pull out all text not contained within tags
using preg_match_all(), and run a str_replace() across these values to
replace any incidences of hyphens, and then another str_replace() to replace
the content with the substring where hyphens were found.
So, something like:
<?
preg_match_all("/(^|>)(.+?)(<|$)/m", $text,$hypharr);
for ($i=0; $i < count($hypharr[0]); $i++){
$rephyph = str_replace("-","-<span style='font-size: 0px;'>
</span>", $hypharr[0][$i]);
if ($rephyph <> $hypharr[0][$i]){
$text= str_replace($hypharr[0][$i],$rephyph,$text);
}
}
?>
This seems to achieve what I'm looking for, ie replacing hyphens when not
contained in HTML tags.
Regards,
Murray