2012-09-17 23 views
1

我試圖用適當的HTML標籤和有麻煩瞄準只是普通鏈接IM更換圖片鏈接,YouTube視頻鏈接和常規鏈接分開: 這裏是我的代碼:我如何捕獲這個正則表達式的鏈接?

function($text) 
    { 
    $output = preg_replace('#(http://([^\s]*)\.(jpg|gif|png))#', 
         '<br/><img src="$1" alt="" width="300px" style = "clear:both;"/><br/>', $text); 

     $output = preg_replace('#(http://([^\s]*)youtube\.com/watch\?v=([^\s]*))#', 
         '<br/><iframe width="480px" height="385px" src="http://www.youtube.com/embed/$3" 
         frameborder="0" allowfullscreen style = "clear:both;"></iframe><br/>', $output); 

    $output = preg_replace('#(http://([^\s]*)\.(com))#', 
         '<br/><a href="$1">$1</a><br/>', $output); 

    return $output 

} 

這不是取代中的所有鏈接最後一步......我如何避免這種情況,並在最後一步中只替換鏈接(不是youtubes或圖像)?

謝謝!

+2

可能重複http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – Amber

+0

與該鏈接imo無關... – algorithmicCoder

回答

1

您尋找的網址需要用空格或換行等分隔符。只需將你的分隔符添加到正則表達式中,所以最後一個正則表達式不會太貪婪!例如...

<?php 

$text = " 
http://www.youtube.com/watch?v=yQ4TPIfCK9A&feature=autoplay&list=FLBIwq18tUFrujiPd3HLPaGw&playnext=1\n 
http://www.asdf.com/asdf.png\n 
http://www.asdf.com\n 
"; 

var_export($text); 
var_export(replace($text)); 

function replace($text) 
{ 
    $output = preg_replace('#(http://([^\s]*)\.(jpg|gif|png))\n#', 
         '<br/><img src="$1" alt="" width="300px" style = "clear:both;"/><br/>', $text); 


    $output = preg_replace('#(http://([^\s]*)youtube\.com/watch\?v=([^\s]*))\n#', 
         '<br/><iframe width="480px" height="385px" src="http://www.youtube.com/embed/$3" 
         frameborder="0" allowfullscreen style = "clear:both;"></iframe><br/>', $output); 

    $output = preg_replace('#(http://([^\s]*)\.(com))\n#', 
         '<br/><a href="$1">$1</a><br/>', $output); 

    return $output; 
} 
+0

我不控制$文本...不能修改它.. – algorithmicCoder

+0

你不應該需要修改它,提供$ text給出以某種方式分隔的url,然後你只是使用該分隔符來區分以.com結尾的URL和以end結尾的URL。 com/asdf.png – user1660098

0

您可以使用preg_replace_callback來爲每個鏈接匹配一次。所以你不必擔心修改兩次鏈接:

$input = <<<EOM 
http://www.example.com/fritzli.jpeg 

https://www.youtube.com/watch?v=blabla+bla+"+bla 

http://wwww.google.com/search?q=blabla 
EOM; 
echo replace($input); 

function replace($text) { 
    $output = preg_replace_callback("#(https?://[^\s]+)#", function($umatch) { 
     $url = htmlspecialchars($umatch[1]); 

     if(preg_match("#\.(jpg|jpeg|gif|png|bmp)$#i", $url)) { 
      $result = "<br/><img src=\"$url\" alt=\"\" width=\"300px\" " 
       . " style = \"clear:both;\"/><br/>"; 
     } elseif(preg_match("#youtube\.com/watch\?v=([^\s]+)#", $url, $match)) { 
      $result = "<br/><iframe width=\"480px\" height=\"385px\"" 
       . " src=\"http://www.youtube.com/embed/{$match[1]}\"" 
       . " frameborder=\"0\" allowfullscreen style = \"clear:both;\">" 
       . "</iframe><br/>"; 
     } else { 
      $result = "<br/><a href=\"$url\">$url</a><br/>"; 
     } 

     return $result; 
    }, $text); 

    return $output; 
} 

注:上面的代碼只能用PHP版本> = 5.3的作品。如果你使用5.3或以下,你可以只提取內部功能到一個單獨的功能,並提供功能名稱作爲參數傳遞給preg_replace_callback

function replace_callback($umatch) { 
    $url = htmlspecialchars($umatch[1]); 

    if(preg_match("#\.(jpg|jpeg|gif|png|bmp)$#i", $url)) { 
     $result = "<br/><img src=\"$url\" alt=\"\" width=\"300px\" " 
      . " style = \"clear:both;\"/><br/>"; 
    } elseif(preg_match("#youtube\.com/watch\?v=([^\s]+)#", $url, $match)) { 
     $result = "<br/><iframe width=\"480px\" height=\"385px\"" 
      . " src=\"http://www.youtube.com/embed/{$match[1]}\"" 
      . " frameborder=\"0\" allowfullscreen style = \"clear:both;\">" 
      . "</iframe><br/>"; 
    } else { 
     $result = "<br/><a href=\"$url\">$url</a><br/>"; 
    } 

    return $result; 
} 

function replace($text) { 
    $output = preg_replace_callback("#(https?://[^\s]+)#", 
     "replace_callback", // the name of the inner function goes here 
     $text); 

    return $output; 
} 
[除XHTML自足標籤的正則表達式匹配開放標籤(的
+0

謝謝!我得到這個錯誤:解析錯誤:語法錯誤,$ output被分配給preg_replace_callback的行上的意外的T_FUNCTION。這對你有用嗎? – algorithmicCoder

+0

啊我相信這個功能需要PHP 5.3和IM 5.2.17 – algorithmicCoder

+0

@algorithmicCoder:嗯,我想你不要使用PHP> = 5.3然後。你可以通過將內部函數移動到一個自己的函數來解決這個問題(比如'function replace_inner($ umatch){...}'),並將「replace_inner」作爲第二個參數傳遞給'preg_replace_callback'。我應該修改我的例子嗎? – vstm