Username:   Password:  

Replace all URLs with TinyUrls in a text

<?php
 
$text = 'foo bar http://maps.google.com/ foo http://somelongurlthatdoesntexist.com bar';
 
if (preg_match_all('|(http://[^s]+)|', $text, $matches, PREG_PATTERN_ORDER)) {
	foreach ($matches[0] as $match) {
		$text = str_replace($match, getTinyUrl($match), $text);
	}
}
echo $text;
 
function getTinyUrl($url) {
	return file_get_contents('http://tinyurl.com/api-create.php?url='.urlencode($url));
}
 
?>

Tags

tinyurl php short url