REGEX match URL in link ignore other tags?

regex

#1

I was using this regex to extract URL from link:

<a href=\"([^\"]*)\">

Works great but now that I added title to the link it doesn't works any more.

<a title="Online dating in España" href="https://www.amigote.com/gente/espana?language_id=1">Online dating España</a>

How to make it work as before and simply ignore title="" stuff?


#2

<a .*?href=\"([^\"]*)\"> should do the trick


#3

Thanks buddy!

How can I grab anchor text from these URLs and add them as title="" attribute to those links?

$question = '<a href="https://www.amigote.com/gente/calle-puerto-rico">Calle Puerto Rico</a>, <a href="https://www.amigote.com/gente/huelva">Huelva</a>, <a href="https://www.amigote.com/gente/andalucia">Andalucía</a>';

I use this but it only matches one link not all:

preg_match('#<a[^>]*>([^<]*)<\/a>#i', $question, $matches);


$link_with_title = str_replace('<a ', '<a title="Online dating in '.$matches['1'].'" ', $matches['0']);

#4

Sounds to me like you’re looking for the preg_replace function :wink:


#5

I am trying for the past 3 hours and can’t find a way please could you help?


#6

Have you looked at preg_replace in the php manual? How far you’ve gotten already applying preg_replace shouldn’t be much of a leap.


#7

But how do I grab anchor text and then add a title=“anchor text” to the link thats what I have no clue?


#8

Can you show the code you’ve tried using preg_replace?


#9

It might be easer to use PHP’s DOM classes to read the HTML, as that automatically does the parsing you try to achieve with a RegExp.