0

Can anyone help me with PHP regex (Regular Expression). I want to get all URLs that matches a certain attribute. The following example is, I want to get all href URLs that has a class of 'foo'.

<a title="foo" href="http://foo.com/" class="foo">Foo</a>
<a href="http://bar.com/" class="bar">Bar</a>
<a class="foo" title="foobar" href="http://foobar.com/">FooBar</a>

Result should be match the 2 URLs:

  • http://foo.com/
  • http://foobar.com/

I know this can be done easily using PHP packages such as DOM crawlers, but I want to use PHP RegEx.

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
paolooo
  • 4,133
  • 2
  • 18
  • 33
  • 2
    Possible duplicate of [RegEx match open tags except XHTML self-contained tags](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – Nir Alfasi Oct 09 '15 at 02:29
  • I'm sorry @alfasin it's not the same, but thank you for the input. Kerwin solve the issue. Thanks. See below. – paolooo Oct 09 '15 at 02:49

1 Answers1

3

See Demo

class="foo"[^>]*href="([^"]*)"[^>]*|href="([^"]*)"[^>]*class="foo"

[^>]*:match other attributes

Kerwin
  • 1,212
  • 1
  • 7
  • 14