I need to get all tag elements in PHP that are starting with a certain string, similar to this:
$dom = new DOMDocument();
$dom->loadHTML( $content );
$domPre = $dom->getElementsByTagName( 'spx-' );
$length = $domPre->length;
return json_encode( $length );
Of course, the above is not working. Any pointers to the right direction would be greatly appreciated, thanks!
Advertisement
Answer
Following the suggestions, this has worked for me:
$dom = new DOMDocument();
$dom->loadHTML( $content );
$xp = new DOMXPath( $dom );
$array = [];
foreach ( $xp->query( "//*[contains(local-name(),'spx')]" ) as $node ) {
array_push( $array, $dom->saveXML( $node ), "n" );
}
return json_encode( count( $array ) );