PHP explode string by html tag

前端 未结 2 475
深忆病人
深忆病人 2021-01-21 21:24

Suppose string $a holds

Phasellus blandit enim eget odio euismod eu dictum quam scelerisque.

Sed ut diam nisi.

Ut v

2条回答
  •  长情又很酷
    2021-01-21 22:05

    Using DOMDocument and DOMXPath (a bit overkill if only a simple solution is needed):

    $dom = new DOMDocument();
    $dom->loadHTML($a);
    $domx = new DOMXPath($dom);
    $entries = $domx->evaluate("//p");
    $arr = array();
    foreach ($entries as $entry) {
        $arr[] = '<' . $entry->tagName . '>' . $entry->nodeValue .  'tagName . '>';
    }
    print_r($arr);
    

提交回复
热议问题