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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.