I need to convert a string of text containing a long url into the same string but with a tinyurl (using the tinyurl api). eg. convert
blah blah blah http://example.com/news/sport blah blah blah
into
blah blah blah http://tinyurl.com/yaeocnv blah blah blah
How can it be done?
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_callbackfunction to your text. This would look something like this: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