检测并转换链接中的url并检测图像并转换为img PHP

Currently, I use this code to detect and convert URL in link in text.

But now, I need to keep this system, but detect and convert image too.

public function convert_to_link($text)
{
    $reg_user = '!@(.+)(?:\s|$)!U';
    if (preg_match_all($reg_user, $text, $matches))
    {
        return preg_replace($reg_user, '<a href="../userdetails.php?uname=$0" title="$0">$0</a>', $text);
    }
    $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
    if(preg_match($reg_exUrl, $text, $url)) {
        // make the urls hyper links
        if (preg_match("/\[img=([^\s'\"<>]+?)\]/i", $text))
        {
           //This is not working
            return preg_replace("/\[img=([^\s'\"<>]+?)\]/i", "<img border=0 src=\"\\1\">", $text);
        }
        else
        {
            return preg_replace($reg_exUrl, '<a href="$0" title="$0">$0</a> ', $text);
        }
    }
    else
    {
        return $text;
    }
}

I use too [img][/img] code to convert image to but with the code located above, the result is bad :

Link

Looks like your regex is looking for this format

[img=http://example.com/name.png]

but your example is in a different format

[img]http://example.com/name.png[/img]

a regex to identify the second form would be

"/^\[img\]([^\[]+)\[\/img\]$/i"