The goal would be: I have some titles. I would like to turn the titles in a text to link. BUT when it is a link, I DON'T want to change. I am looking for the right regex.
I have a PHP code like this:
foreach($res as $r) {
$new_string = '<a href="#" onclick="getMarkers(1,\\\' \\\',1);locate('.$r->latitude.','.$r->longitude.','.$r->zoom.','.$r->id.',0);">$0</a>';
$introduction = (preg_replace("/\b$r->title\b(?![^<>]*(?:<|$))/i",$new_string,$introduction))
}
This part of my code doesn't work:
preg_replace("/\b$r->title\b(?![^<>]*(?:<|$))/i",$new_string,$introduction)
The problem is: This regex also change the avilable links what is in HTML tag.
Thank you for everybody patiente and I am wainting for the answers!
Thanks!
UPDATE: I would like to say thank you for HamZa for this fantastic link!
My solutions is:
$introduction = (preg_replace("/[^>]*>.*?<\/a>(*SKIP)(*FAIL)|$r->title/im",$new_string,$introduction));
Thanks for everybody! :)
This may be an overly simple solution, but you can use a negative lookbehind to make sure that the url does not have a
href=
in front of it. If not, then capture it with whatever domain REGEX you prefer.I used a pretty clunky domain name validator, so this is going to look like a mess, but I'll explain it.
This outputs:
Okay, now an explanation of the REGEX:
There really only 2 parts to this:
href="
,href='
(escaped, of course) orhref=
in front of it.