Let's say $ content is the content of the text area
$content = preg_replace('!((https://|http://)+[a-z0-9_./?=&-]+)!i', '<a target="_blank" href="$1">$1</a> ', nl2br($_POST['helpcontent'])." "); $content = preg_replace('!((www\.)+[a-z0-9_./?=&-]+)!i', '<a target="_blank" href="http://$1">$1</a> ', $content." ");
This worked fine for links, but realized that it breaks the markup when the image is inside the text ...
I'm trying it like this:
$content = preg_replace('!\s((https?://|http://)+[a-z0-9_./?=&-]+)!i', ' <a href="$1">$1</a> ', nl2br($_POST['content'])." "); $content = preg_replace('!((www\.)+[a-z0-9_./?=&-]+)!i', '<a target="_blank" href="http://$1">$1</a> ', $content." ");
Like images, but the problem is that url with the format http: // or https: // will no longer be converted ..:
google.com β Not converted (as expected)
www.google.com β Well Converted
http://google.com β Not Converted (unexpectedly)
https://google.com β Not Converted (unexpectedly)
What am I missing?
-Edit -
Current near-working solution:
$content = preg_replace('!(\s|^)((https?://)+[a-z0-9_./?=&-]+)!i', ' <a href="$2" target="_blank">$2</a> ', nl2br($_POST['content'])." "); $content = preg_replace('!(\s|^)((www\.)+[a-z0-9_./?=&-]+)!i', '<a target="_blank" href="http://$2" target="_blank">$2</a> ', $content." ");
The fact is that if this is an input:
www.funcook.com http://www.funcook.com https://www.funcook.com funcook.com http://funcook.com https://funcook.com
All the urls I want (all except name.domain) translate as expected, but this is the output
www.funcook.com http://www.funcook.com https://www.funcook.com; funcook.com http://funcook.com https://funcook.com
Pay attention to a; inserted, any idea why?