I have the below HTML string, and I would like to turn it into an array.
$string = \'
1
Pass the node to DOMDocument::saveHTML to get its HTML representation:
$string = '
1
2
3
4
';
$dom = new DOMDocument;
$dom->loadHTML($string);
foreach($dom->getElementsByTagName('a') as $node)
{
$array[] = $dom->saveHTML($node);
}
print_r($array);
Result:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
Only works with PHP 5.3.6 and higher, by the way.