Tuesday, 10 May 2016

PHP RegEx Grouping Multiple Matches

I'm just trying my hand at crafting my very first regex. I want to be able to match a pseudo HTML element and extract useful information such as tag name, attributes etc.:



$string = '';

if (preg_match('/<(\w+?)(\s\w+?\s*=\s*".*?")+\s*>/', $string, $matches)) {
print_r($matches);
}



Except, I'm getting:



Array ( [0] =>  [1] => testtag [2] => gamma="abc" ) 


Anyone know how I can get the other attributes? What am I missing?

No comments:

Post a Comment