Search code examples
phpregexattributeshtml-parsing

Get attribute assignment expressions from `<a>` tag


I have a string like this:

< a href = 'howdy.html' class     =  'pt-nght' >

and I want to get an array that contains:

array[0] = "href = 'howdy.html'"

array[1] = "class = 'pt-nght'"

I tried to use this regex:

[^<\s*(.*)]\s*(.*)\s*=\s*(.*)\s*[^>]

but the result seems to be far away from what I expect. Maybe there is another technique?


Solution

  • You may have a better time using DOMDocument, but you could also try:

    [^\s]+?\s*=\s*'.*?'