Skip to content Skip to sidebar Skip to footer

Extract Html Attributes In Php With Regex

I want to get HTML attributes from string with PHP but fail with: $string = '
    '; preg_filter('/(\w[-\w]*)=\'(.*?)\'/', '$1',

Solution 1:

Don't use regexes for parsing HTML

$string = '<ul id="value" name="Bob" custom-tag="customData">';
$dom = new DOMDocument();
@$dom->loadHTML($string);
$ul = $dom->getElementsByTagName('ul')->item(0);
echo $ul->getAttribute("id");
echo $ul->getAttribute("name");
echo $ul->getAttribute("custom-tag");

Post a Comment for "Extract Html Attributes In Php With Regex"