I wish to have a function that I could parse in text and then it will replaces all the links that contain of (jpg|png|gif|jpeg|bmp) extension with <img>
tag, after that it will also replaces all the other links without (jpg|png|gif|jpeg|bmp) extension with <a>
tag.
For example it should replaces:
http://imgur.com/gallery/TpGvHBL http://i.imgur.com/TpGvHBL.jpg
to
<a href="http://imgur.com/gallery/TpGvHBL" target="_blank">http://imgur.com/gallery/TpGvHBL</a> <img src="http://i.imgur.com/TpGvHBL.jpg" />
===========================================================================
Currently I'm able to replace image url to <img>
tag using below regex:
$text = preg_replace('#((https?|ftp):\/\/([^\s]*)\.(jpg|gif|png))#', '<img src="$1" />', $text);
and also below to replace normal url to <a>
tag:
$text = preg_replace('/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/i', '<a href="$1" target="_blank">$1</a>', $text);
What I want is to change the second regex to replace non image url only, as it will conflicted with my first regex.
Thank you.
\b
to prevent false matches 5) enjoy – HamZa