Jump to content

Big url to readable url


The Little Guy

Recommended Posts

I have this piece of code:

	if(preg_match("~(http://(.+?))(.\h)~", $list, $matches)){
	$m = parse_url($matches[1]);
	$host = trim(strtolower($m['host']), 'www.');
	$list = preg_replace("~http://(.+?)(.\h)~", '<a href="'.$matches[1].'">'.$host.'</a>$2', $list);
}

 

It takes a url and converts to a nice readable url.

for example, if I use this url:

http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=cat

 

it will convert it to this:

google.com

 

The problem is, that it doesn't convert urls if they are at the end of a line.

 

What would the best way be to match ALL urls (know matter where they are in the context) and allow me to make them more readable (like the way I have now)?

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.