Shorten urls automatically with a Zend Framework filter

I think we can all agree that URL shortening services are great and are very handy to tidy up those long and obnoxious links. However, a lot of the time people simply forget to use them, or often don’t know about them in the first place. I’ve noticed this in a blog system I wrote using Zend Framework. On one hand I love that people post messages, but on the other it annoys me that they may supply a link that is so long it breaks the formatting of the page, or looks just plain ugly.

So what are my options? I could train everyone who posts blogs on the system to use a url shortening service or I could manually tweak all the links myself. As solutions they are not very practical at all; I don’t have the time to change any/all links myself, and I certainly don’t have enough patience to train everyone! So an automatic way of doing things is needed, and the filtering in Zend Framework comes to the rescue!


Zend Framework has a lot of filters that you can use right out of the box. They do all sorts such as string tags, newlines, camel case strings, and so on. The framework also provides an interface so you can easily create your own filters. For example, converting a string to title case. With that in mind you could see how easy it’d be to create a filter to scan for urls and shorten them. And that’s exactly what this filter does! It will scan the supplied text for any http or https urls, and then, if they are past a certain size, it will attempt to connect to a url shortening service and then replace the long urls with the short.

/**
 * Shorten urls found in text.
 *
 * This filter will scan the supplied text for any http or https urls, and
 * then, if they are past a certain size, it will attempt to connect to a
 * url shortening service and then replace the long urls with the short.
 *
 * Two services endpoints are in the class - tinyurl and is.gd - but there's
 * nothing to stop you supplying a different end point, so long as the format
 * of the end point has one string directive (%s) and the service simply
 * returns a short url.
 *
 * @copyright Copyright (c) 2009, Andrew Collington 
 * @license New BSD License
 */
class Amnuts_Filter_ShortenUrls implements Zend_Filter_Interface
{
    const REGEX = "~(?:https?://(?:(?:(?:(?:(?:[a-zA-Z\d](?:(?:[a-zA-Z\d]|-)*[a-zA-Z\d])?)\.)*(?:[a-zA-Z](?:(?:[a-zA-Z\d]|-)*[a-zA-Z\d])?))|(?:(?:\d+)(?:\.(?:\d+)){3}))(?::(?:\d+))?)(?:/(?:(?:(?:(?:[a-zA-Z\d$\-_.+!*'(),]|(?:%[a-fA-F\d]{2}))|[;:@&=])*)(?:/(?:(?:(?:[a-zA-Z\d$\-_.+!*'(),]|(?:%[a-fA-F\d]{2}))|[;:@&=])*))*)(?:\?(?:(?:(?:[a-zA-Z\d$\-_.+!*'(),]|(?:%[a-fA-F\d]{2}))|[;:@&=])*))?)?)~";
    const SERVICE_ISGD    = 'http://is.gd/api.php?longurl=%s';
    const SERVICE_TINYURL = 'http://tinyurl.com/api-create.php?url=%s';

    /**
     * Options array containing:
     *
     *    - maxsize
     *      Maximum size the url can be before attempting to shorten it
     *    - service
     *      The url to the service API that returns a short url
     *    - timeout
     *      The maximum time allowed for a connection
     *
     * @var array
     */
    public $options = array(
        'maxsize' => 35,
        'service' => self::SERVICE_ISGD,
        'timeout' => 5
    );

    /**
     * Set options on construct
     *
     * @param array $options
     */
    public function __construct(array $options = array())
    {
        if (!empty($options)) {
            $this->options = array_merge($this->options, $options);
        }
    }

    /**
     * Filter the text.
     *
     * The filtering may take some time, depending on how many links are in
     * the text to be shortened, how responsive the service is, etc.
     *
     * @param string $text
     * @return string
     * @see Zend_Http_Client, Zend_Http_Response
     */
    public function filter($text)
    {
        $matches = $replacements = array();
        $matched = preg_match_all(self::REGEX, $text, $matches, PREG_PATTERN_ORDER);
        if ($matched) {
            $http = new Zend_Http_Client();
            foreach ($matches[0] as $url) {
                if (strlen($url) > $this->options['maxsize']) {
                    $http->setConfig(array(
                        'timeout' => $this->options['timeout']
                    ));
                    $http->setUri(
                        sprintf($this->options['service'], urlencode($url))
                    );
                    $response = $http->request(Zend_Http_Client::GET);
                    if ($response->isSuccessful()) {
                        $replacements[$url] = $response->getBody();
                    }
                    $http->resetParameters();
                }
            }
        }
        if (empty($replacements)) {
            return $text;
        }
        return str_replace(
            array_keys($replacements),
            array_values($replacements),
            $text
        );
    }
}

It’s very easy to use, too:

$shorten = new Amnuts_Filter_ShortenUrls();
$updated = $shorten->filter($text);

Here’s an example of using tinyURL as the service and only converting urls in the text that are longer than 50 characters:

$shorten = new Amnuts_Filter_ShortenUrls(array(
    'maxsize' => 50,
    'service' => Amnuts_Filter_ShortenUrls::SERVICE_TINYURL
));
$updated = $shorten->filter($text);
Did you like this? Share it:

Leave a Reply