Re: replace
| From: | Monte Ohrt | Date: | Sat, 06 Jan 2001 17:07:49 +0000 |
| Subject: | Re: replace | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-33037@lists.php.net to get a copy of this message | ||
Probably the safest way to do this is to first split out all HTML tags, then
do a search and replace on the text, then reassemble it. Something like this
might work:
// $document contains your html page
// split off html tags
$htmltags = _striphtml($document);
// split off non-html tags
$htmlwords = _splithtml($document);
// do your replacement on non-html content
$htmlwords = preg_replace("a","",$htmlwords);
// reassemble the document
$document = _assemblehtml($htmltags,$htmlwords);
function _striphtml($document)
{
// return all html markup and javascript blocks in an array
preg_match_all("'<script[^>]*?>.*?</script>|<[\/\!]*?[^<>]*?>|\&\w+\;'si",
$document, $tags);
return $tags[0];
}
function _splithtml($document)
{
// return all data around html tags and javascript blocks in an array
$htmlwords =
preg_split("'<script[^>]*?>.*?</script>|<[\/\!]*?[^<>]*?>|\&\w+\;'si",
$document);
return $htmlwords;
}
function _assemblehtml($tags,$body)
{
for($x=0; $x <= count($body); $x++)
{
$combinedtext .= $body[$x];
$combinedtext .= $tags[$x];
}
return $combinedtext;
}
Mark McIntyre wrote:
> Is it possible to replace all strings in a document, unless that string is
> within a tag? A quick example: Say I want to delete all occurances of the
> letter 'a', but want to leave links intact. That is:
>
> <a href="aaa.html">this is a fantastic test</a>
>
> should become:
>
> <a href="aaa.html">this is fntstic test</a>
>
> Any help will be appreciated. :)
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net