Jump to content


Photo

plain text to clickable short URL


  • Please log in to reply
4 replies to this topic

#1 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 29 August 2006 - 10:49 PM

Not as simple as you might think.

I have plain text in a database and want any URL strings to be clickable when displayed.  The URLs in the text can be in the form http://www.mysite.com, http://mysite.com, or www.mysite.com.  The 'trick' is that many of the URLs are extremely long and I need the clickable part of the display to shorten the link to, say, 30 characters max.

<a href='http://mysite.com/wombats/stuff/show_me.php?this=6&that=7&furry=yes'>http://mysite.com/wombats  ....furry=yes'></a>

I assume some preg wizardy can solve this, hence my post.

edit: these links are all offsite, so getting target='_blank' into the output would be a definite bonus.
Legend has it that reading the manual never killed anyone.
My site

#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 30 August 2006 - 02:12 PM

Here's one for starting out. You can expand it by adding .net, .org, etc.

<pre>
<?php

	$tests = array(
		'http://www.mysite.com',
		'http://mysite.com',
		'www.mysite.com',
		'http://mysite.com/wombats/stuff/show_me.php?this=6&that=7&furry=yes',
		'text without an url',
		'url with www.google.com in text',
	);

	function clickable_short_url ($matches) {
		### Entire match.
		$url = array_shift($matches);
		### Only http:// match, if any.
		$http = array_shift($matches);
		### Shorten textual part if need be.
		$url_text = strlen($url) >= 30 ?
			(substr($url, 0, 20) . '...' . substr($url, -10, 10)) :
			$url ;
		### Add http:// prefix if need be.
		if (! $http) {
			$url = 'http://' . $url;
		}
		### Return new url.
		return '<a target="_blank" href="' . $url . '">' . $url_text . '</a>';
	}
	
	$pattern = '%
		(http://)? ### optional http:// prefix
		(?(1)|www\.) ### require www. if there is no http://
		\S+ ### gobble anything but white space
	%x';
	
	foreach ($tests as $test) {
		echo preg_replace_callback($pattern, 'clickable_short_url', $test);
		echo '<br/>';
	}
?>
</pre>

Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#3 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 30 August 2006 - 02:38 PM

;D - exactly.  Thanks for that.

One micro-quibble. If the text being processed is something like " ... or did I find the solution at phpfreaks.com?" the closing question mark appears as part of the URL and in the link text displayed. I ought to be able to hack and slash my way through the code to fix that (fingers crossed).
Legend has it that reading the manual never killed anyone.
My site

#4 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 30 August 2006 - 04:57 PM

	$pattern = '%
		(http://)? ### optional http:// prefix
		(?(1)|www\.) ### require www. if there is no http://
		\S+ ### gobble anything but white space
		(?=\b) ### followed by a word boundary
	%x';

Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#5 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 30 August 2006 - 05:12 PM

Thank you. That's amazing.
Legend has it that reading the manual never killed anyone.
My site




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users