匹配并替换捕获/匹配的字符

I currently have a code that finds and replaces urls into complete html links. It works fine but now i need to update it so that if there is image url then it should convert it into a html img tag and display it. Function im using now is...

function auto_link_text($text) {

   $pattern  = '#\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))#';
   $callback = create_function('$matches', '
       $url       = array_shift($matches);
       $url_parts = parse_url($url);



       return sprintf(\'<a rel="nowfollow" target="_blank" href="%s">%s</a>\', $url, $url);
   ');

   return preg_replace_callback($pattern, $callback, $text);
}

Got it from... How to add anchor tag to a URL from text input

Here is an example of the text i would to it to go through...

asdf
http://google.com/
asfd
http://yahoo.com/logo.jpg
http://www.apple.com/sdfsd.php?page_id=13&id=18210&status=active#1
http://youtube.com/logo.png

like it updated function to output...

asdf
<a rel="nowfollow" target="_blank" href="http://google.com/">http://google.com/</a>
asfd
<img src="http://yahoo.com/logo.jpg" class="example">
<a rel="nowfollow" target="_blank" href="http://www.apple.com/sdfsd.php?page_id=13&id=18210&status=active#1">http://www.apple.com/sdfsd.php?page_id=13&id=18210&status=active#1</a>
<img src="http://youtube.com/logo.png" class="example">

Big thanks in advance!

You can use this for example:

function create_anchor_tag($url, $text = false) {
    if ($text===false) $text = $url;
    return '<a rel="no-follow" target="_blank" href="' . $url . '">'
         . $text . '</a>'; 
}

function create_image_tag($url) {
    return '<img src="' . $url . '"/>';
}

function auto_link_text($text) {
    $pattern = '~\b(?:(?:ht|f)tps?://|www\.)\S+(?<=[\PP?])~i';

    $callback = function ($m) {
        $img_ext = array('jpg', 'jpeg', 'gif', 'png');
        $path = parse_url($m[0], PHP_URL_PATH);
        $ext = substr(strrchr($path, '.'), 1);

        if (in_array(strtolower($ext), $img_ext))
            return create_image_tag($m[0]);
        return create_anchor_tag($m[0]); 
    };

    return preg_replace_callback($pattern, $callback, $text);
}

I used several functions to make it more clea[rn], but you can easily adapt it as you like.

Here is the nice post about the best suitable regex pattern for valid URL. I picked one from there to group all the URLs.

Online demo

Steps to follow:

  1. simply extract the url.
  2. put a check on the url and based on your own logic substitute the tag as shown in demo.

sample code: (get all the valid urls in groups. get it from index 1)

$re = "/(([A-Za-z]{3,9}:(?:\\/\\/)?(?:[-;:&=\\+\\$,\\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\\+\\$,\\w]+@)[A-Za-z0-9.-]+)((?:\\/[\\+~%\\/.\\w-_]*)?\\??(?:[-\\+=&;%@.\\w_]*)#?(?:[\\w]*))?)/";
$str = "...";

preg_match_all($re, $str, $matches); 

sample code: (substitute anchor tag (or what ever you want to add))

$re = "/(([A-Za-z]{3,9}:(?:\\/\\/)?(?:[-;:&=\\+\\$,\\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\\+\\$,\\w]+@)[A-Za-z0-9.-]+)((?:\\/[\\+~%\\/.\\w-_]*)?\\??(?:[-\\+=&;%@.\\w_]*)#?(?:[\\w]*))?)/";
$str = "...";
$subst = '<a href="$1">$1</a>';

$result = preg_replace($re, $subst, $str);