Php remove html element from string

I am having trouble working out how to do this, I have a string looks something like this...

    $text = "<p>This is some example text This is some example text This is some example text</p>
             <p><em>This is some example text This is some example text This is some example text</em></p>
             <p>This is some example text This is some example text This is some example text</p>";

I basically want to use something like preg_repalce and regex to remove

<em>This is some example text This is some example text This is some example text</em>

So I need to write some PHP code that will search for the opening <em> and closing </em> and delete all text in-between

hope someone can help, Thanks.

asked Sep 23, 2011 at 13:24

lukehillonlinelukehillonline

2,2101 gold badge30 silver badges46 bronze badges

3

$text = preg_replace('/([\s\S]*)(<em>)([\s\S]*)(</em>)([\s\S]*)/', '$1$5', $text);

answered Sep 23, 2011 at 13:36

6

In case if you are interested in a non-regex solution following would aswell:

<?php
    $text = "<p>This is some example text This is some example text This is some example text</p>
             <p><em>This is some example text This is some example text This is some example text</em></p>
             <p>This is some example text This is some example text This is some example text</p>";


    $emStartPos = strpos($text,"<em>");
    $emEndPos = strpos($text,"</em>");

    if ($emStartPos && $emEndPos) {
        $emEndPos += 5; //remove <em> tag aswell
        $len = $emEndPos - $emStartPos;

        $text = substr_replace($text, '', $emStartPos, $len);
    }

?>

This will remove all the content in between tags.

answered Sep 23, 2011 at 13:42

NertimNertim

3806 silver badges15 bronze badges

4

$text = '<p>This is some example text This is some example text This is some example text</p>
<p><em>This is the em text</em></p>
<p>This is some example text This is some example text This is some example text</p>';

preg_match("#<em>(.+?)</em>#", $text, $output);

echo $output[0]; // This will output it with em style
echo '<br /><br />';
echo $output[1]; // This will output only the text between the em

[ View output ]

For this example to work, I changed the <em></em> contents a little, otherwise all your text is the same and you cannot really understand if the script works.

However, if you want to get rid of the <em> and not to get the contents:

$text = '<p>This is some example text This is some example text This is some example text</p>
<p><em>This is the em text</em></p>
<p>This is some example text This is some example text This is some example text</p>';

echo preg_replace("/<em>(.+)<\/em>/", "", $text);

[ View output ]

answered Sep 23, 2011 at 13:34

Php remove html element from string

Kalle H. VäravasKalle H. Väravas

3,5294 gold badges29 silver badges46 bronze badges

5

Use strrpos to find the first element and then the last element. Use substr to get the part of string. And then replace the substring with empty string from original string.

answered Sep 23, 2011 at 13:41

Php remove html element from string

markomarko

10.3k16 gold badges69 silver badges91 bronze badges

3

  format: $text = str_replace('<em>','',$text);
$text = str_replace('</em>','',$text);

answered Sep 23, 2011 at 13:33

Php remove html element from string

2

How can I remove part of a string in PHP?

The substr() and strpos() function is used to remove portion of string after certain character. strpos() function: This function is used to find the first occurrence position of a string inside another string. Function returns an integer value of position of first occurrence of string.

Is it possible to remove the HTML tags from data?

Strip_tags() is a function that allows you to strip out all HTML and PHP tags from a given string (parameter one), however you can also use parameter two to specify a list of HTML tags you want.

How do I remove text tags in HTML?

The HTML tags can be removed from a given string by using replaceAll() method of String class. We can remove the HTML tags from a given string by using a regular expression. After removing the HTML tags from a string, it will return a string as normal text.

What is strip HTML?

stripHtml( html ) Changes the provided HTML string into a plain text string by converting <br> , <p> , and <div> to line breaks, stripping all other tags, and converting escaped characters into their display values.