0

How can I split the different links.

<ul>
<li><a href="/page/?link1=2&page=2#fff">pag2 </a></li><li><a href="/page/">pag2 </a></li><li><a href="/page/">pag2 </a></li>
</ul>

I try something like that, but it didn't find it at all

$re = '/<a href=["\'](([^\'\"\#]*)?)(?=[\'"\#])(.*)["\']>[^<\/a>]+<\/a>/mi';
$str = '<ul>
<li><a href="/page/">pag2 </a></li><li><a href="/page/">pag2 </a></li><li><a href="/page/">pag2 </a></li>
</ul>


';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

// Print the entire match result
var_dump($matches);

Also try this, but it capture it all

$re = '/<a href=["\'](([^\'\"\#]*)?)(?=[\'"\#])(.*)["\']>.+<\/a>/mi';
boaz
  • 25
  • 6
  • Do you know that you don't need RegExes for this and shouldn't go with them? – revo Feb 14 '18 at 14:30
  • 3
    [H̸̡̪̯ͨ͊̽̅̾̎Ȩ̬̩̾͛ͪ̈́̀́͘ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜Ȇ̴̟̟͙̞ͩ͌͝S̨̥̫͎̭ͯ̿̔̀ͅ](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) - use a parser – ctwheels Feb 14 '18 at 14:37
  • @ctwheels - He's stripping out links from a string. The identifier is the `href`. You don't need to parse it here, you just need to globally find `href` and capture it's contents. You don't even need to look at the surrounding tags. https://meta.stackoverflow.com/questions/252390/the-famous-tag-regex-answer-should-its-title-be-edited – KyleFairns Feb 14 '18 at 14:56

1 Answers1

1
\shref=(["'])(.*?)\1

Breakdown:

  • \s - a space character, as not to match <other-prefix>-href="example.com"
  • href= - match href= literally
  • (["']) - capture the type of quote used (group 1)
  • (.*?) capture everything until (group 2)
  • \1 - you find the type of quote used (matches what was matched in group 1)

See the demo

KyleFairns
  • 2,947
  • 1
  • 15
  • 35