Jump to content

removing a tag and everything inbetween


michaellunsford

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?
Link to comment
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.
Link to comment
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?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.