SReca SReca - 7 months ago 42
PHP Question

Use regex preg_match to find opening anchor tag and grab class attribute

What I am simply trying to do is take the following code:

$Anchors = '<a href="#" class="test1"><div class="test2"><a href="#" class="test3"><div class="test4">'

And get the value of the class attribute of the last anchor tag, in this case "test3." So far I have this:


But obviously its not doing what I want it to do, any help?

Answer Source


This regular expression will:

  • match the last anchor tag from your string
  • capture the value for the class attribute
  • avoid many of the potential problems with using regex to when searching html strings


.*<a\b(?=\s) # capture the open tag
(?=(?:[^>=]|='[^']*'|="[^"]*"|=[^'"][^\s>]*)*?\sclass=['"]([^"]*)['"]?)  # capture the src attribute value
(?:[^>=]|='[^']*'|="[^"]*"|=[^'"\s]*)*"\s?\/?> # get the entire  tag

enter image description here


Live example here:

Sample text

note that the last tag has a difficult edge case

<a href="#" class="test1"><div class="test2">
<a onmouseover=' class="NotTheClass" ; funClassRotator(class) ; ' class="test3" href="#" ><div class="test4">

Capture Groups

[0][0] = <a href="#" class="test1"><div class="test2"><a href="#" onmouseover=' class="NotTheClass" ; funClassRotator(class) ; ' class="test3">
[0][1] = test3
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download