1

I want to select a tag without custom class in my string have another a tag cannot select that's, String :

<h3 class="r"><a dir="rtl" class="sla" href="#"><span>test<span></a></h3>
<h3>test1</h3>
<h3 class="r"><a class="test" href="#">test2</a></h3>
<h3>test3</h3>

my Reg:

preg_match_all('@<h3\s*class="r">\s*<a[^<>]*href="([^<>]*)"[^<>]*>(.*)</a>\s*</h3>@siU',
        $file, $matches);

how i can select all in pattern a tag without class="sla"

lock
  • 711
  • 3
  • 9
  • 19

1 Answers1

1

Use regexp "negative lookahead" <a(?![^<>]*sla)... will discard all results with all "sla" inside "a" tag.

preg_match_all('@<h3\s*class="r">\s*<a(?![^<>]*sla)[^<>]*href="([^<>]*)"[^<>]*>(.*)<\/a>\s*<\/h3>@siU',
$file, $matches)

Also you can use more precise statement: <a(?![^<>]*class=\"sla\")...

Additional information about the "lookaround" expressions: http://www.regular-expressions.info/lookaround.html

Evgeniy Maynagashev
  • 690
  • 1
  • 8
  • 13