Username:   Password:  

Create TinyURL with PHP

function getTinyUrl($url) {
	return file_get_contents('http://tinyurl.com/api-create.php?url='.urlencode($url));
}
 
// Example Use:
$url = 'http://google.com';
echo getTinyUrl($url);

This PHP function uses the TinyURL API to create a shorter URL.

Tags

PHP TinyURL function

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

Convert shortened URL back to original URL

<?php
function getNormalUrl($url) {
	$url = trim($url);
	$h = get_headers($url, true);
	return $h['Location'];
}
?>

This PHP function takes a shortened URL (ie TinyURL, bit.ly, etc) and converts it back to the original URL.

Tags

URL PHP web TinyURL bit ly