Preg_replace URL but not images

$title = $_POST['title']; $post = stripslashes($_POST['TextArea']); $link = preg_replace('"(http://www\S+)"','<a href="$1">$1</a>', $post); echo $link; 

After submitting my form above the script, replace all links inside textarea and the result for the images should be broken.

Is there a way to replace links, but not images?

While url works fine, result for images in browser

 <img src="<a href="http://...myimage.jpg"">http://.../myimage.jpg"</a> height="150" width="150"> 

enter image description hereenter image description here

thanks

+4
source share
2 answers
 preg_replace('"(?<!src=[\"\'])(http://www\S+)"','<a href="$1">$1</a>', $text) 

This converts only http://www tags that are not preceded by src=" or src=' .

+4
source
 preg_replace('/(?<!src=[\"\'])(http(s)?:\/\/(www\.)?[\/a-zA-Z0-9%\?\.\-]*)(?=$|<|\s)/','<a href="$1">$1</a>', $text); 

This is the right way because the previous solution does not complete the url if necessary

0
source

All Articles