Created
July 8, 2012 09:20
-
-
Save rodneyrehm/3070128 to your computer and use it in GitHub Desktop.
PHP: parse HTML element attributes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function parseAttributes($text) { | |
$attributes = array(); | |
$pattern = '#(?(DEFINE) | |
(?<name>[a-zA-Z][a-zA-Z0-9-:]*) | |
(?<value_double>"[^"]+") | |
(?<value_single>\'[^\']+\') | |
(?<value_none>[^\s>]+) | |
(?<value>((?&value_double)|(?&value_single)|(?&value_none))) | |
) | |
(?<n>(?&name))(=(?<v>(?&value)))?#xs'; | |
if (preg_match_all($pattern, $text, $matches, PREG_SET_ORDER)) { | |
foreach ($matches as $match) { | |
$attributes[$match['n']] = isset($match['v']) | |
? trim($match['v'], '\'"') | |
: null; | |
} | |
} | |
return $attributes; | |
} | |
//$text = '<a double="google.com" keyword single=\'google.com\' none=google.com>'; | |
$text = 'double="double.com" keyword single=\'single.com\' none=none.com'; | |
$res = parseAttributes($text); | |
var_dump($res); |
@rickhellewell Try changing line 12
from: (?<n>(?&name))(=(?<v>(?&value)))?#xs';
to: (?<n>(?&name))(\s*=\s*(?<v>(?&value)))?#xs';
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello! Stumbled (actually, googled) upon this. Nice regex; but doesn't parse elements that might have spaces around the '=', as in ' title = "something" ' (any number of spaces around/after the = character).
Adjustment?
Thanks...Rick..