我需要获取此iframe代码的vimeo ID,这是我的字符串。
$str = '<iframe src="http://player.vimeo.com/video/34134823?title=0&byline=0" width="900" height="506" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>'
我相信ID部分34134823
可能多于或少于8个字符,因此我无法使用索引
我可以使用2次爆炸,一次用于http://player.vimeo.com/video/
而不是一次用于?
问号,但我确信使用正则表达式会更好,只有我是一个菜鸟... < / p>
答案 0 :(得分:2)
为什么不使用这两种爆炸?当然,你可以用正则表达式来做。但是考虑到URL的定义方式,我首先要删除第一个?
之后的所有内容,然后删除剩下的/
之前的所有内容。
虽然有些人认为regexp优雅(事实上,我甚至可能自己使用它),但strpos
和strrpos
在这里同样快速可靠。
$end = strpos($test, "?") - 1;
$sta = strrpos(substr($test, 0, $end), "/") + 1;
$ret = substr($test, $sta, $end);
答案 1 :(得分:1)
<?php
$str = '<iframe src="http://player.vimeo.com/video/34134823?title=0&byline=0" width="900" height="506" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
$stat = preg_match('/.*video\/(\d{1,})\?title.*/', $str, $matches);
print_r($matches);
答案 2 :(得分:1)
$pattern = '/\<iframe\s+.*src\=\"?[^\"]*player\.vimeo\.com\/video\/([\d]{5,})\??.*\".*\<\/iframe\>/i';
if(preg_match($pattern, $iframe_tag, $result)){
$vimeo_id = $result[1];
}