JSHINER Posted October 1, 2007 Share Posted October 1, 2007 <?php function clickable_short_url ($matches) { $url = array_shift($matches); $http = array_shift($matches); $url_text = strlen($url) >= 30 ? (substr($url, 0, 50)) : $url ; if (! $http) { $url = 'http://' . $url; } return '<a target="_blank" href="' . $url . '">My Website</a>'; } $pattern = '% (http://)? ### optional http:// prefix (?(1)|www\.) ### require www. if there is no http:// \S+ ### gobble anything but white space %x'; echo preg_replace_callback($pattern, 'clickable_short_url', $page['person']['website']); ?> The above is what I used to take http://www.site.com and www.site.com and convert them to clickable URL's within other text. Now how can I make it so if the value of $website = site.com or www.site.com to have it automatically add either http://www. or http:// to the front of that? Link to comment https://forums.phpfreaks.com/topic/71409-solved-formatting-a-url/ Share on other sites More sharing options...
The Little Guy Posted October 1, 2007 Share Posted October 1, 2007 This should do the trick: Modified: <?php $url = 'http://www.mysite.com'; if(!preg_match("~^http://www.(.+)$~",$url)){ $url = 'http://www.'.$url; $url = preg_replace("~http://www.http://~","http://www.",$url); } echo $url; ?> Link to comment https://forums.phpfreaks.com/topic/71409-solved-formatting-a-url/#findComment-359424 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.