3

Suppose string $a holds

<p>Phasellus blandit enim eget odio euismod eu dictum quam scelerisque.
</p><p>Sed ut diam nisi.</p><p>Ut vestibulum volutpat luctus.</p>

How can I explode this into this array

Array(
    [0] = '<p>Phasellus blandit enim eget odio euismod eu dictum quam scelerisque.</p>';
    [1] = '<p>Sed ut diam nisi. Ut vestibulum volutpat luctus.</p>';
    [2] = '<p>Ut vestibulum volutpat luctus.</p>';
)

2 Answers 2

8

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);
Sign up to request clarification or add additional context in comments.

1 Comment

You may consider calling libxml_use_internal_errors(true); first to avoid DOMDocument polluting your standard error handler with HTML related errors. Implement your own handler to catch the errors instead. See the comments in php.net/manual/de/domdocument.loadhtml.php for more information.
7
<?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

3 Comments

preg is faster for basic parsing like this.
we are speaking about php and its slow. anyway, you can save a couple of miliseconds on it but loose a couple of hours thying to make this code working with more complex strings.
Nice demo ~ but this is not the correct way ~ parsing html with RegEx is a recipe for disaster...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.