2011-12-29 45 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