I have some HTML with custom attributes and trying to parse it with component PHPHtmlParser. Whole project created via this component. Here is the problem example given.
use PHPHtmlParser\Dom;
class Parsemydiv {
function parseAttr()
{
$str='<div otop="20" oleft="20" name="info">
<img src="example.jpg">
</div>';
$dom = new Dom();
$dom->loadStr($str);
$otop = $dom->getAttribute("otop");
$name = $dom->getAttribute("name");
echo "Name: " . $name . PHP_EOL;
echo "Top: " . $otop . PHP_EOL;
echo "Left: " . $oleft . PHP_EOL;
}
}
Output is: Name: info Top: Left:
getAttribute cannot get custom attributes.
Why use a 3rd party library to parse the DOM when PHP has built-in support for this? I suggest learning the native functions instead:
$str='<div otop="20" oleft="15" name="info">
<img src="example.jpg">
</div>';
$doc = new DOMDocument();
$doc->loadHTML($str);
$div = $doc->getElementsByTagName('div')[0];
$otop = $div->getAttribute('otop');
$oleft = $div->getAttribute('oleft');
echo "otop=$otop, oleft=$oleft"; //otop=20, oleft=15