Reputation: 41
$title = $_POST['title'];
$post = stripslashes($_POST['TextArea']);
$link = preg_replace('"(http://www\S+)"','<a href="$1">$1</a>', $post);
echo $link;
After submit my form the above script replace all links inside textarea and the result for images is to be broken.
Is there a way to replace links but not images?
While url works perfect the result for images in browser is
<img src="<a href="http://...myimage.jpg"">http://.../myimage.jpg"</a> height="150" width="150">
Thank you
Upvotes: 4
Views: 1363
Reputation: 337
preg_replace('/(?<!src=[\"\'])(http(s)?:\/\/(www\.)?[\/a-zA-Z0-9%\?\.\-]*)(?=$|<|\s)/','<a href="$1">$1</a>', $text);
This is the correct way because previous solution does not finish url when necessary
Upvotes: 0
Reputation: 191819
preg_replace('"(?<!src=[\"\'])(http://www\S+)"','<a href="$1">$1</a>', $text)
This will only convert http://www
links that are not preceded by src="
or src='
.
Upvotes: 4