In order to shorten an arbitrary number of URLs in your text, put the API stuff in a function which takes the long URL and returns the short URL. Then apply this function via PHP's preg_replace_callback
function to your text. This would look something like this:
<?php
function shorten_url($matches) {
// EDIT: the preg function will supply an array with all submatches
$long_url = $matches[0];
// API stuff here...
$url = "http://tinyurl.com/api-create.php?url=$long_url";
return file_get_contents($url);
}
$text = 'I have a link to http://www.example.com in this string';
$textWithShortURLs = preg_replace_callback('|http://([a-z0-9?./=%#]{1,500})|i', 'shorten_url', $text);
echo $textWithShortURLs;
?>
Don't count on that pattern too much, just wrote it on-the-fly without any testing, maybe someone else can help.
See http://php.net/preg-replace-callback