PHP explode string by html tag

前端 未结 2 389
深忆病人
深忆病人 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 21:49
    <?php
    $ps    = array();
    $count = preg_match_all('/<p[^>]*>(.*?)<\/p>/is', $a, $matches);
    for ($i = 0; $i < $count; ++$i) {
        $ps[] = $matches[0][$i];
    }
    

    That could be one way. Or you could use a loop with strpos

    0 讨论(0)
  • 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 .  '</' . $entry->tagName . '>';
    }
    print_r($arr);
    
    0 讨论(0)
提交回复
热议问题