How do I remove

tag and its content from HTML using php

后端 未结 7 1206
既然无缘
既然无缘 2021-02-06 06:22

Below is the text I need to remove

tags from

Addiction, stress and subjective wellbeing

The need and signi

相关标签:
7条回答
  • 2021-02-06 06:25

    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);
    
    0 讨论(0)
  • 2021-02-06 06:39
    echo "<div id=\"main_content\">" . strip_tags($row[main_content]) . "</div>";
    
    0 讨论(0)
  • 2021-02-06 06:41

    You must 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?

    0 讨论(0)
  • 2021-02-06 06:45
    $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>');
    
    0 讨论(0)
  • 2021-02-06 06:48

    just to remove p tags you can do this

    $text=str_ireplace('<p>','',$text);
    $text=str_ireplace('</p>','',$text);    
    
    0 讨论(0)
  • 2021-02-06 06:49

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

    0 讨论(0)
提交回复
热议问题