9

Below is the text I need to remove <p> tags from

<p> Addiction, stress and subjective wellbeing</p> 
<p> The need and significance of traditional shop lot pavements in the context of town conservation in Malaysia</p> 
<p> The role of wage and benefit in engaging employee commitment</p>

I tried this

$title= preg_replace('#<p>(.*?)</p>#', '', $title['result']);**

But still am Getting <p> tags, any ideas?

Ilia Ross
  • 13,086
  • 11
  • 53
  • 88
spsaravananct
  • 392
  • 2
  • 4
  • 17
  • In case you're looking to remove specific tags without removing content, you might find this `strip_tags_only` function helpful: https://gist.github.com/aalaap/0176bc6b2df799038f711cb6718dcddc – aalaap Jul 06 '18 at 11:58

9 Answers9

22

You should use this regular expression to catch <p> tag and all of its content:

'/<p\b[^>]*>(.*?)<\/p>/i'

Working example to catch and remove <p> tag and all of its content:

$title = "<div>Text to keep<p class='classExample'>Text to remove</p></div>";
$result = preg_replace('/<p\b[^>]*>(.*?)<\/p>/i', '', $title);
echo $result;

Please see live demo on Codepad

If you want to use regular expressions to parse HTML - then you will not be able to do this.

Read more about your matter here: How do you parse and process HTML/XML in PHP?

Ilia Ross
  • 13,086
  • 11
  • 53
  • 88
  • $title = preg_replace('%

    ]*>\s*(\[([^\[\]]+)\].*?\[/\2\])\s*

    %s', '', $result['title']); Is is Correct
    – spsaravananct Aug 26 '12 at 14:38
  • 1
    Use this in your case: `$title = preg_replace('/

    ]*>(.*?)<\/p>/i', '', $result['title']);`

    – Ilia Ross Aug 26 '12 at 15:02
  • 1
    If you want to strip the tags, in myu opinion, it means that you want to keep the content of the tag, but not the tag itself. To do so, you need to add \1 in the second parameter of the preg_replace function : $result = preg_replace('/

    ]*>(.*?)<\/p>/i', '\1', $title);

    – Gfra54 Jan 29 '15 at 15:54
7

Try:

If you want to remove <p> tag only

$html = "
<p> Addiction, stress and subjective wellbeing</p> 
<p> The need and significance of traditional shop lot pavements town</p> 
<p> The role of wage and benefit in engaging employee commitment</p>
";

echo strip_tags($html);

If you want to remove <p> tags and its content

$html = preg_replace('#\<p>[{\w},\s\d"]+\</p>#', "", $html);
MD SHAHIDUL ISLAM
  • 14,325
  • 6
  • 82
  • 89
6

just to remove p tags you can do this

$text=str_ireplace('<p>','',$text);
$text=str_ireplace('</p>','',$text);    
Tarun
  • 3,162
  • 3
  • 29
  • 45
  • If you do just that then 1. It will only be able catch empty p tag such as `

    ` and; 2. Content of this tag will stay.

    – Ilia Ross Aug 26 '12 at 15:19
  • then use regular expression – Tarun Sep 15 '14 at 10:23
  • -1 because it is not a definitive solution. You have to use regular expressions as in the answer below. – Marco Panichi Oct 31 '14 at 18:30
  • 2
    @MarcoPanichi If answer given by me solved the problem of questioner then how does that cause problem to you. If your problem cant be solve using the above solution then raise your question in stackoverflow instead of down vote my answer. – Tarun Nov 01 '14 at 06:15
3
echo "<div id=\"main_content\">" . strip_tags($row[main_content]) . "</div>";
Porta Shqipe
  • 766
  • 7
  • 8
2

If you just need to strip all the markup, go with strip_tags().

moonwave99
  • 21,957
  • 3
  • 43
  • 64
2
$text = '<p>Test paragraph.</p> <a href="#fragment">Other text</a>';
echo strip_tags($text);
echo "\n";

// Allow p and a tag
echo strip_tags($text, '<p><a>');
Mark
  • 6,762
  • 1
  • 33
  • 50
Ramesh
  • 1,495
  • 12
  • 14
0

This code removes one p-tag:

function parse($html) {
    $dom = new DOMDocument();
    @$dom->loadHTML($html);
    $ps = $dom->getElementsByTagName('p');
    //die('<pre>'.print_r($ps,1).'</pre>');
    if ($ps->length === 1){
        $p = $ps->item(0);
        return $p->nodeValue;
    } else {
        return '';
    }
}
CGN
  • 579
  • 4
  • 13
0

Here is another example to strip all <p> tags with or without the closing / after the first <

$content = '<p> Addiction, stress and subjective wellbeing</p>
<p> The need and significance of traditional shop lot pavements in the context of town conservation in Malaysia</p>
<p> The role of wage and benefit in engaging employee commitment</p>';
    $new_content = preg_replace('/<(|\/)p>/', '', $content);
    dump($new_content);
    die('HERE');

Output

Nicholas
  • 77
  • 10
0
$data1=html_entity_decode("<p>your text with p tag</p>");
$data2=str_ireplace('<p>',' ',$data1);
$data3=str_ireplace('</p>',' ',$data2); 
echo $data3;
Aswani
  • 1
  • 1