I have the following string:
"Look on http://www.google.com".
I need to convert it to:
"Look on http://www.google.com"
The original string can have more than 1 URL string.
How do I do this in php?
Thanks
You can use the following:
$string = "Look on http://www.google.com"; $string = preg_replace( "~[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]~", "<a href=\"\\0\">\\0</a>", $string);
PHP versions < 5.3 (ereg_replace) otherwise (preg_replace)
lib_autolink
does a pretty good job, avoiding pitfalls like extra punctuation after the link and links inside HTML tags:
https://github.com/iamcal/lib_autolink
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With