如何使用正则表达式从 HTML 获取所有 YouTube iframe
How to get all YouTube iframe from HTML using regex
我想使用正则表达式获取所有 YouTube iframe
,并想为找到的每条记录添加特定标签。
例如<youtube-frame></youtube-frame>
到iframe开始和结束。
需要输出:
<youtube-frame><iframe width="560" height="315" src="https://www.youtube.com/embed/vakfMRyjulw" frameborder="0" allowfullscreen></iframe></youtube-frame>
<youtube-frame><iframe width="560" height="315" src="https://www.youtube.com/embed/aDGWMlKPKDs" frameborder="0" allowfullscreen></iframe></youtube-frame>
我的代码
$embed = '
<iframe width="560" height="315" src="https://www.youtube.com/embed/vakfMRyjulw" frameborder="0" allowfullscreen></iframe>
<iframe width="600" height="350" src="https://tune.pk/player/embed_player.php?vid=6508414&folderp2016/05/04/&width=600&height=350&autoplay=no" frameborder="0" allowfullscreen scrolling="no"></iframe>
<iframe width="560" height="315" src="https://www.youtube.com/embed/aDGWMlKPKDs" frameborder="0" allowfullscreen></iframe>
<iframe width="600" height="350" src="https://tune.pk/player/embed_player.php?vid=6508414&folder=2016/05/04/&width=600&height=350&autoplay=no" frameborder="0" allowfullscreen scrolling="no"></iframe>
<iframe width="600" height="350" src="https://tune.pk/player/embed_player.php?vid=6508414&folder=2016/05/04/&width=600&height=350&autoplay=no" frameborder="0" allowfullscreen scrolling="no"></iframe>
';
我试过什么?
$pattern = '/<iframe\.*src=\"//youtube"\.*/';
$iframeSrc = preg_match($pattern, $embed, $matches);
var_dump($iframeSrc);
试试这个:
$iframeSrc = preg_replace('/<iframe[^>]*src\s*=\s*"?https?:\/\/[^\s"\/]*youtube.com(?:\/[^\s"]*)?"?[^>]*>.*?<\/iframe>/i', '<youtube-frame>[=10=]</youtube-frame>', $embed);
这使用 preg_replace
和全局正则表达式将所有 YouTube IFrame 标记(包括它们的结束标记)替换为 <youtube-frame>[=12=]</youtube-frame>
,其中 [=13=]
是原始字符串。
如果您完全确定输入的格式,理论上可以简化正则表达式,但我将其设计为足够强大以应对其他语法,例如 src=http://example.com
或 src = "http://example.com"
等. 现在被浏览器接受,它只匹配 *.youtube.com
域上的源而不是 myyoutubesite.com
.
我想使用正则表达式获取所有 YouTube iframe
,并想为找到的每条记录添加特定标签。
例如<youtube-frame></youtube-frame>
到iframe开始和结束。
需要输出:
<youtube-frame><iframe width="560" height="315" src="https://www.youtube.com/embed/vakfMRyjulw" frameborder="0" allowfullscreen></iframe></youtube-frame>
<youtube-frame><iframe width="560" height="315" src="https://www.youtube.com/embed/aDGWMlKPKDs" frameborder="0" allowfullscreen></iframe></youtube-frame>
我的代码
$embed = '
<iframe width="560" height="315" src="https://www.youtube.com/embed/vakfMRyjulw" frameborder="0" allowfullscreen></iframe>
<iframe width="600" height="350" src="https://tune.pk/player/embed_player.php?vid=6508414&folderp2016/05/04/&width=600&height=350&autoplay=no" frameborder="0" allowfullscreen scrolling="no"></iframe>
<iframe width="560" height="315" src="https://www.youtube.com/embed/aDGWMlKPKDs" frameborder="0" allowfullscreen></iframe>
<iframe width="600" height="350" src="https://tune.pk/player/embed_player.php?vid=6508414&folder=2016/05/04/&width=600&height=350&autoplay=no" frameborder="0" allowfullscreen scrolling="no"></iframe>
<iframe width="600" height="350" src="https://tune.pk/player/embed_player.php?vid=6508414&folder=2016/05/04/&width=600&height=350&autoplay=no" frameborder="0" allowfullscreen scrolling="no"></iframe>
';
我试过什么?
$pattern = '/<iframe\.*src=\"//youtube"\.*/';
$iframeSrc = preg_match($pattern, $embed, $matches);
var_dump($iframeSrc);
试试这个:
$iframeSrc = preg_replace('/<iframe[^>]*src\s*=\s*"?https?:\/\/[^\s"\/]*youtube.com(?:\/[^\s"]*)?"?[^>]*>.*?<\/iframe>/i', '<youtube-frame>[=10=]</youtube-frame>', $embed);
这使用 preg_replace
和全局正则表达式将所有 YouTube IFrame 标记(包括它们的结束标记)替换为 <youtube-frame>[=12=]</youtube-frame>
,其中 [=13=]
是原始字符串。
如果您完全确定输入的格式,理论上可以简化正则表达式,但我将其设计为足够强大以应对其他语法,例如 src=http://example.com
或 src = "http://example.com"
等. 现在被浏览器接受,它只匹配 *.youtube.com
域上的源而不是 myyoutubesite.com
.