Hi all,
I'm trying to match attributes from a html tag, but I can't get it working :)
Let's take this tag for example:
<a href="ddd" class='sw ' w'>
Obviously the last part is not quite right.
Now I tried to match the attributes part with this piece of code:
preg_match('/(\s+\w+=(?P<quote>(\'|\"))[^(?P=quote)]*(?P=quote))*/U', " href=\"bla\" class='sw'sw'", $a);
Here $a is empty, and that's what I expect. But if I now take my complete expression it does match the last class part, which puzzles me. It looks like this:
preg_match('/<(?P<c>[\/]?)(?P<tag>\w+)(?P<atts>(\s+\w+=(?P<quote>(\'|\"))[^(?P=quote)]*(?P=quote))*)\s*(?P<sc>[\/]?)>/U', $tag, $a);
Now $a returns:
Array
(
[0] => <a href="ddd" class='sw ' w'>
[c] =>
[1] =>
[tag] => a
[2] => a
[atts] => href="ddd" class='sw ' w'
[3] => href="ddd" class='sw ' w'
[4] => class='sw ' w'
[quote] => '
[5] => '
[6] => '
[sc] =>
[7] =>
)
Notice the key 4 which contains the class part including the last 'w, while I did use the (U)ngreedy switch at the end.
Any clues?