Sharing long urls by email or on social media sites is never a good idea, this is why most people are using urls shorteners such as biy.ly or Tinyurl. This function takes a single parameter, an url, and will return a short url using the Tinyurl service.
function getTinyUrl($url) {
return file_get_contents("http://tinyurl.com/api-create.php?url=".$url);
}
Usage
Nothing complicated, simply pass an url to the function to get its short url:
$my_url = getTinyUrl('http://www.phpsnippets.info/test-existence-of-a-given-url-with-curl');
echo 'Short url: '.$my_url;
If you want to use this snippet within WordPress, you can do so by using the following within the loop:
getTinyUrl(get_permalink($post->ID));

One Comment
Useful! Thanks
2 Trackbacks
[...] This post was mentioned on Twitter by Jean-Baptiste Jung, PHP Snippets. PHP Snippets said: PHP snippet: Convert url to TinyURL http://bit.ly/bsIA3z #php [...]
[...] { return file_get_contents("http://tinyurl.com/api-create.php?url=".$url); }Source: http://www.phpsnippets.info/convert-url-to-tinyurlCalculate age using date of birthPass a birth date to this function, and it will return the age of [...]