2011-12-29 44 views
0

我有這樣的代碼的preg_match和preg_replace函數的YouTube網址

preg_match_all('%(?:youtube\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $asd, $match); 

找到像

<a href="http://www.youtube.com/watch?v=">http://www.youtube.com/watch?v=ZiJRPREeQ1Q</a> 
     <br /> 
     <a href="http://www.youtube.com/watch?v=GaEZgqxPHLs&feature=related">http://www.youtube.com/watch?v=GaEZgqxPHLs&feature=related</a> 

這項工作優良網址的YouTube鍵查找代碼ZiJRPREeQ1Q和GaEZgqxPHLs,現在我想更換所有新的代碼

想在html線使用

preg_replace 

找到整個YouTube網址

<a href="*">*</a> 

到一個新的代碼,我該怎麼辦呢?

-------------- --------------增加

後,我通過preg_math_all 從網址查看YouTube的代碼,我使用該代碼提取碼

foreach($match[1] as $youtube){ 
      // $youtube; // this handle the youtube code 
      $match = ""; // what can i write here relative to $youtube ? 
      $str .= preg_replace($match, 'new code',$content); // $content handle the whole thread that contain the youtube url <a href=*>*</a>         
     } 

,我唯一需要的是什麼,我可以使用,以取代YouTube的代碼

+1

請參閱[這裏](http://stackoverflow.com/questions/2129443/any-preg-match-to-check-if-a-url-is-a-youtube-vimeo-dailymotion-video-link )和[這裏](http://stackoverflow.com/questions/1416425/preg-replace-preg-match-for-href-in-html-link) – sooper 2011-12-29 00:44:23

+1

如果你想「使用」preg_replace,那麼你將有了解它是如何工作的。這是完全可行的,但不適合初學者。 (如果你只是想要一些現成的代碼,搜索可能會出現一些東西。) – mario 2011-12-29 00:53:13

+0

我更新我的文章,請閱讀它 – 2011-12-29 01:24:01

回答

1
$html = file_get_contents($url); //or curl function  
$re="<link itemprop=\"embedURL\" href=\"(.+)\">"; 
preg_match_all("/$re/siU", $html, $matches); 
$youtube = $matches[1][0]; 

正則表達式
$html = file_get_contents($url); //or curl function  
$re="<link itemprop=\"url\" href=\"(.+)\">"; 
preg_match_all("/$re/siU", $html, $matches); 
$youtube = $matches[1][0]; 
+0

這不會取代!我想replcae的YouTube代碼從到另一個代碼像新的 – 2011-12-29 00:49:01

+0

你能舉個例子嗎? – 2011-12-29 00:53:13

+0

我更新我的文章,請閱讀 – 2011-12-29 01:24:22