Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - want to check if user input has website address in it

I want that whenver a user inserts "www." in a comment textarea, the address from "www." until the first space will be a replaced with a link to that address:

"I love www.google.com"
turns into
"I love <a href="www.google.com">www.google.com</a>"

Can you please tell me how to do this? (newbie)

(sorry for posting the earlier question I still don't quite get it).

Should I use preg_match_all()?

like image 898
Gal Avatar asked Dec 01 '25 09:12

Gal


2 Answers

Try:

$text = preg_replace('/(www\.[a-zA-Z0-9-]+\.[a-zA-Z\.]{2,})/', '<a href="http://\\1">\\1</a>', $text);
like image 89
Ozzy Avatar answered Dec 04 '25 01:12

Ozzy


preg_replace('/www\.(*)\.com/',"<a href='www.$1.com'>www.$1.com</a>",$strUrl);

BAH beat me to the punch.

like image 38
strubester Avatar answered Dec 04 '25 00:12

strubester



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!