我正在尝试使用preg_match从嵌入代码中提取YouTube视频的宽度。
代码类似于:
<iframe title="YouTube video player" width="480" height="390" src="http://www.youtube.com/embed/3uw-nUvGrBY" frameborder="0" allowfullscreen></iframe>我想提取width="480"。
我试过了:
/width="[0-9]+"/
/width=\"[0-9]+\"/
/width=.[0-9]+./但它们都不起作用。如果我去掉最后的双引号和数字,我可以提取width=",但不能超出这个范围。
发布于 2011-04-01 04:47:04
$string = '<iframe title="YouTube video player" width="480" height="390" src="http://www.youtube.com/embed/3uw-nUvGrBY" frameborder="0" allowfullscreen></iframe>';
preg_match('/.*width="(\d+)".*/', $string, $match);\
echo $match[1];但我确信,PHP或JS DOM能够更轻松地完成这项工作……
发布于 2011-04-01 04:50:24
您的实际源代码的格式可能会有所不同。您的示例html代码片段看起来太典型了。试试这个:
preg_match('#\b(width\s*=)\s*["\']?(\d+)#i', $source, $match);
print $match[2];发布于 2011-04-01 04:57:39
$ptn = "/(?<=width=\")\d*(?=\")/";
$str = '<iframe title="YouTube video player" width="480" height="390" src="http://www.youtube.com/embed/3uw-nUvGrBY" frameborder="0" allowfullscreen></iframe>';
preg_match($ptn, $str, $matches);
echo $matches[0]; // 480https://stackoverflow.com/questions/5506088
复制相似问题