2017-03-01 7 views
0

現在、このコードを使用して、リンクのURLを検出してテキストに変換します。リンクのURLを検出して変換し、画像を検出してimgに変換するPHP

しかし、私はこのシステムを維持する必要がありますが、イメージも検出して変換します。

public function convert_to_link($text) 
{ 
    $reg_user = '[email protected](.+)(?:\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; 
    } 
} 

私が画像を変換するために、あまりにも[IMG] [/ IMG]コードを使用しますが、上記の位置のコードで、結果は悪いです:あなたの正規表現が探しているよう

Link

答えて

0

が見えますこの形式

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

が、あなたの例では、異なるフォーマットである

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

私は別の方法を試し

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

だろう第二の形式を識別するための正規表現と同じ結果[IMG =]または[IMG] [/ IMG]は – ewan

+0

は、あなたが何と一緒にサンプル入力を提供することができますアウトプットを期待していますか? – ChrisG

関連する問題