我有这个代码
preg_match_all('%(?:youtube'.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu'.be/)([^"&?/ ]{11})%i', $asd, $match);
查找网址的YouTube密钥,例如
<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>
这很好找到代码 ZiJRPREeQ1Q 和 GaEZgqxPHL ,现在我想用新代码替换所有 html 行
想要使用
preg_replace
查找整个 YouTube 网址
<a href="*">*</a>
到新代码,我该怎么做?
--------------增加--------------
在我通过 url 获得 youtube 的代码后preg_math_all我使用此代码提取代码
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代码的正则表达式
$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];