如何使用p php删除p标签及其内容?

时间:2014-03-13 16:25:57

标签: php web-scraping simple-html-dom

我使用simple_html_dom废弃网站,它有一个这样的部分 $ content =

<div id="content">

    <p>
    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Odio, voluptas, sint, accusantium, 
    </p>

    Wikipedia (Listeni/ˌwɪkɨˈpiːdiə/ or Listeni/ˌwɪkiˈpiːdiə/ wik-i-pee-dee-ə) is a collaboratively edited, multilingual, free Internet encyclopedia that is supported by <br>
    the non-profit Wikimedia Foundation. Volunteers worldwide collaboratively write Wikipedia's 30 million articles in 287 languages, including over 4.4 <br>
    million in the English Wikipedia. Anyone who can access the site can edit almost any of its articles, which on the Internet <br>

    <p>
    quidem repellendus nulla incidunt ullam?    
    </p>

</div>

但我只希望结果是这样的。

Wikipedia (Listeni/ˌwɪkɨˈpiːdiə/ or Listeni/ˌwɪkiˈpiːdiə/ wik-i-pee-dee-ə) is a collaboratively edited, multilingual, free Internet encyclopedia that is supported by <br>
the non-profit Wikimedia Foundation. Volunteers worldwide collaboratively write Wikipedia's 30 million articles in 287 languages, including over 4.4 <br>
million in the English Wikipedia. Anyone who can access the site can edit almost any of its articles, which on the Internet <br>

我知道这个发现并在#content部分中提取内容。我只想知道如何删除其中的p标签和内容。

谢谢。

2 个答案:

答案 0 :(得分:0)

$content = 'hi <p> this is a test </p> hello <p> the other test </p>';

echo removeP($content);

function removeP($content){

    while(strpos($content, '<p>') != ''){

        $start = strpos($content, '<p>');
        $end = strpos($content, '</p>')+4;

        $string_being = substr($content, 0, $start);
        $string_end = substr($content, $end, strlen($content));

        $content = $string_being.$string_end;

    }

    return $content;
}

答案 1 :(得分:0)

要简单地删除p标签,您可以这样做:

foreach($doc->find('p') as $p) $p->outertext = '';