Jump to content

Archived

This topic is now archived and is closed to further replies.

michaellunsford

removing a tag and everything inbetween

Recommended Posts

I'm looking to strip the <style> tag, which includes about ten lines between the opening and closing of the tag.

So, a simple example: [code=php:0]striptags("<a href='mailto:somebody@example.com'>somebody@example.com</a>");[/code] returns somebody@example.com

how does one remove the whole thing? from <a to </a?

Share this post


Link to post
Share on other sites
Try this one:
[code]$text = "ABC<a href='mailto:somebody@example.com'>somebody@example.com</a>XYZ";
$pattern = '/(\<a)(.+?)(\<\/a\>)/i';
echo preg_replace($pattern,"", $text);[/code]

Ronald  8)

Share this post


Link to post
Share on other sites
that's what I'm looking for, but I can't seem to get it to work with the < script and < /script > tags. php.net talks about using a patern, but there isn't a patern reference. Where can I learn how to read your patern and make my own?

Share this post


Link to post
Share on other sites
look in the PHP manual:

Example 2. Using indexed arrays with preg_replace()

<?php
$string = 'The quick brown fox jumped over the lazy dog.';
$patterns[0] = '/quick/';
$patterns[1] = '/brown/';
$patterns[2] = '/fox/';
$replacements[2] = 'bear';
$replacements[1] = 'black';
$replacements[0] = 'slow';
echo preg_replace($patterns, $replacements, $string);
?>
The above example will output:

The bear black slow jumped over the lazy dog.

By ksorting patterns and replacements, we should get what we wanted.

<?php
ksort($patterns);
ksort($replacements);
echo preg_replace($patterns, $replacements, $string);
?>
The above example will output:

The slow black bear jumped over the lazy dog.

Share this post


Link to post
Share on other sites
Ahh, but the intricacies of replacing a tag and everything in-between is considerably more complex -- thus the question.

so, more specifically: when preg_replace sees this as a patern '/(\<a)(.+?)(\<\/a\>)/i' what is it understanding?  And, why wouldn't replacing the instances of the "a" with "style" not work?

Share this post


Link to post
Share on other sites
ahh, found a regexp website. apparently, since the style spans several lines, and dot doesn't interpret newlines by default, I either had to add an "s" pattern modifyer, or use [/s/S]+ instead of .+

all is well. Thanks for the help!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.