Jump to content

[SOLVED] Formatting a URL


JSHINER

Recommended Posts

<?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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.