This question already has an answer here:
I need regular expression that converts links in plain text to HTML links. my code is:
preg_replace('/(((f|ht){1}tps:\/\/)[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/i',
'<a href="\\1" target="_blank">\\1</a>', $text);
but this expression will make the image url to href as well. so my question is how to avoid if the url is in img tag like.
The text: https://yahoo.com this is my image <img src="https://img.com/img.jpg">
The result with my expression:<a href="https://yahoo.com">https://yahoo.com</a> this is my image <img src="<a href="https://img.com/img.jpg">https://img.com/img.jpg</a>">
i want this one <a href="https://yahoo.com">https://yahoo.com</a> this is my image <img src="http://img.com/img.jpg">
</div>
It's possible using negative lookbehind operator (?<!text)
, though probably not the most efficient way since the engine will back track a lot. Maybe you could do strip_tags('img')
before preg_replace
?
Another drawback of lookbehind is that it has to be fixed length. This means you cannot grab onto img
since there may be additional attributes between it and the src
. Anyhow, if you're really tempted to use it, your regex would look like this
preg_replace('/(?<!src=[\'"])(((f|ht){1}tps?:\/\/)[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/i',
'<a href="\\1" target="_blank">\\1</a>', $text);
Also, no need for second expression, as Gavriel points out, just add an s?
Why do you have it twice? The only difference I see is the s in the https, but that you could achieve like this:
preg_replace('/(((f|ht){1}tps?:\/\/)[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/i',
'<a href="\\1" target="_blank">\\1</a>', $text);
IMHO you got the result because the 1st line did what you wanted for and the second line "did it once again". If your input has the link and the img url in one line then you could maybe combine the 2 regexes to 1 long one that catches both. That way the 2nd each "half" of the regex will only replace it's "part" of the line