JustinK101 Posted August 29, 2010 Share Posted August 29, 2010 Looking for help writing a function that does the following: Look for http:// or https:// in a string and replaces it with an <a href> tag. For example: Here is my link http://www.mydomain.com some further text here. Becomes: Here is my link <a href="http://www.mydomain.com">http://www.mydomain.com</a> some further text here. Link to comment https://forums.phpfreaks.com/topic/211985-function-to-automatically-create-links/ Share on other sites More sharing options...
redarrow Posted August 29, 2010 Share Posted August 29, 2010 preg_replace(); Link to comment https://forums.phpfreaks.com/topic/211985-function-to-automatically-create-links/#findComment-1104775 Share on other sites More sharing options...
sohaibshaheen Posted August 29, 2010 Share Posted August 29, 2010 Looking for help writing a function that does the following: Look for http:// or https:// in a string and replaces it with an <a href> tag. For example: Here is my link http://www.mydomain.com some further text here. Becomes: Here is my link <a href="http://www.mydomain.com">http://www.mydomain.com</a> some further text here. This id pretty simple. All you have to do is: $text="hello I am doing well http://www.google.com ok"; preg_match("/[a-zA-Z]+[:\/\/]+[A-Za-z0-9\-_]+\\.+[A-Za-z0-9\.\/%&=\?\-_]+/i",$text,$url); $new= "<a href=\"$url[0]\">$url[0]</a>"; echo preg_replace("/[a-zA-Z]+[:\/\/]+[A-Za-z0-9\-_]+\\.+[A-Za-z0-9\.\/%&=\?\-_]+/i",$new,$text); And you are done... Link to comment https://forums.phpfreaks.com/topic/211985-function-to-automatically-create-links/#findComment-1104776 Share on other sites More sharing options...
jcbones Posted August 29, 2010 Share Posted August 29, 2010 Un-tested function stringToAnchor($str) { return preg_replace('~((mailto\:|(news|(ht|f)tp(s?))\://){1}\S+)~','<a href="$1">$1</a>',$str); } //Testing echo stringToAnchor('You can find the best deals at http://www.google.com'); Link to comment https://forums.phpfreaks.com/topic/211985-function-to-automatically-create-links/#findComment-1104779 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.