Jump to content


Photo

removing a tag and everything inbetween


  • Please log in to reply
5 replies to this topic

#1 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 15 September 2006 - 03:11 PM

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:
striptags("<a href='mailto:somebody@example.com'>somebody@example.com</a>");
returns somebody@example.com

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

#2 ronverdonk

ronverdonk
  • Members
  • PipPipPip
  • Advanced Member
  • 277 posts
  • LocationNetherlands

Posted 15 September 2006 - 04:46 PM

Try this one:
$text = "ABC<a href='mailto:somebody@example.com'>somebody@example.com</a>XYZ";
$pattern = '/(\<a)(.+?)(\<\/a\>)/i';
echo preg_replace($pattern,"", $text);

Ronald  8)
RTFM is an almost extinct art form, it should be subsidized.

#3 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 15 September 2006 - 06:46 PM

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?

#4 markbett

markbett
  • Members
  • PipPipPip
  • Advanced Member
  • 133 posts

Posted 15 September 2006 - 06:48 PM

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.

#5 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 15 September 2006 - 07:06 PM

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?

#6 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 15 September 2006 - 08:43 PM

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!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users