title: how to find image only links in string and linkify them
body: <p>I have this code: <a href="https://gist.github.com/jasny/2000705" rel="nofollow">https://gist.github.com/jasny/2000705</a>
  How would I modify it to only work on links that end with jpg, gif, bmp, etc?</p>
  
  <p>Original code:</p>
  
  <pre><code>&lt;?php
      /**
       * Turn all URLs in clickable links.
       * 
       * @param string $value
       * @param array  $protocols  http/https, ftp, mail, twitter
       * @param array  $attributes
       * @param string $mode       normal or all
       * @return string
       */
      public function linkify($value, $protocols = array('http', 'mail'), array $attributes = array())
      {
          // Link attributes
          $attr = '';
          foreach ($attributes as $key =&gt; $val) {
              $attr = ' ' . $key . '="' . htmlentities($val) . '"';
          }
  
          $links = array();
  
          // Extract existing links and tags
          $value = preg_replace_callback('~(&lt;a .*?&gt;.*?&lt;/a&gt;|&lt;.*?&gt;)~i', function ($match) use (&amp;$links) { return '&lt;' . array_push($links, $match[1]) . '&gt;'; }, $value);
  
          // Extract text links for each protocol
          foreach ((array)$protocols as $protocol) {
              switch ($protocol) {
                  case 'http':
                  case 'https':   $value = preg_replace_callback('~(?:(https?)://([^\s&lt;]+)|(www\.[^\s&lt;]+?\.[^\s&lt;]+))(?&lt;![\.,:])~i', function ($match) use ($protocol, &amp;$links, $attr) { if ($match[1]) $protocol = $match[1]; $link = $match[2] ?: $match[3]; return '&lt;' . array_push($links, "&lt;a $attr href=\"$protocol://$link\"&gt;$link&lt;/a&gt;") . '&gt;'; }, $value); break;
                  case 'mail':    $value = preg_replace_callback('~([^\s&lt;]+?@[^\s&lt;]+?\.[^\s&lt;]+)(?&lt;![\.,:])~', function ($match) use (&amp;$links, $attr) { return '&lt;' . array_push($links, "&lt;a $attr href=\"mailto:{$match[1]}\"&gt;{$match[1]}&lt;/a&gt;") . '&gt;'; }, $value); break;
                  case 'twitter': $value = preg_replace_callback('~(?&lt;!\w)[@#](\w++)~', function ($match) use (&amp;$links, $attr) { return '&lt;' . array_push($links, "&lt;a $attr href=\"https://twitter.com/" . ($match[0][0] == '@' ? '' : 'search/%23') . $match[1]  . "\"&gt;{$match[0]}&lt;/a&gt;") . '&gt;'; }, $value); break;
                  default:        $value = preg_replace_callback('~' . preg_quote($protocol, '~') . '://([^\s&lt;]+?)(?&lt;![\.,:])~i', function ($match) use ($protocol, &amp;$links, $attr) { return '&lt;' . array_push($links, "&lt;a $attr href=\"$protocol://{$match[1]}\"&gt;{$match[1]}&lt;/a&gt;") . '&gt;'; }, $value); break;
              }
          }
  
          // Insert all link
          return preg_replace_callback('/&lt;(\d+)&gt;/', function ($match) use (&amp;$links) { return $links[$match[1] - 1]; }, $value);
      }
  </code></pre>