PHP 帮助了解 preg_match 模式
这是我精心开发的插件的核心。虽然我在模式方面遇到了一些麻烦..
<?php
$s = '
<embed type="application/x-shockwave-flash" id="single2" name="single2" src="http://api.realitylapse.com/player.swf" allowscriptaccess="always" allowfullscreen="true" wmode="transparent" flashvars="file=http://cerium.realitylapse.com/stream/bea352a230ebd36b52dc27d874f41f5a/4e3c5eca/default/xxxxx/xxxxx-lq.mp4&plugins=ltas&ltas.cc=inhldvymihzxqln&provider=http" height="424" width="659">
<embed type="application/x-shockwave-flash" src="http://www.xxxxx.com/player9397/player.swf?" quality="high" allowfullscreen="true" allowscriptaccess="always" wmode="opaque" flashvars="provider=http&file=http://www.xxxxx.com/player9397/vb.php?id=TT175YivmF4y&type=video&backcolor=111111&frontcolor=cccccc&lightcolor=DE4949&stretching=fill" height="420" width="99%">
<embed src="http://www.megavideo.com/v/P5X0UOA267fb79acd04cdb29a057c3fa0066573a1" type="application/x-shockwave-flash" allowfullscreen="true" width="100%" height="438">
';
$patterns = array();
$patterns[] = '<embed[^>]+src=["\'](.+?)["\']';
$patterns[] = '<embed[^>]+data=["\'](.+?)["\']';
$patterns[] = '<embed[^>]+flashvars="(.+?)["\']'; //Possible problem..
$patterns[] = '<embed[^>]+file=(.+?)[&]'; //
$patterns[] = '<iframe[^>]+src=["\'](.+?)["\']';
$patterns[] = '<iframe[^>]+data=["\'](.+?)["\']';
$patterns[] = '<object[^>]+src=["\'](.+?)["\']';
$patterns[] = '<object[^>]+data=["\'](.+?)["\']';
$patterns[] = '<video[^>]+src=["\'](.+?)["\']';
$patterns[] = '<video[^>]+data=["\'](.+?)["\']';
$patterns[] = '<video[^>]+file=(.+?)[&]';
$patterns = "#(?:" . implode("|", $patterns) . ")#si";
preg_match_all($patterns, ($s), $m);
if (!empty($m[0]))
{
$edata = array();
foreach($m[0] as $match)
{
//Embeds:
if (preg_match('#realitylapse.com/stream/(.+?)[&,"\']#si', $match, $id))
$edata[] = "<!--nextpage--><!--tab_title:CERIUM-->\n[cerium " . $id[1] . "]";
else if (preg_match('#http&file=http://www.xxxx.com/player9397/vb.php?id=(.+?)[&,"\']#si', $match, $id))
$edata[] = "<!--nextpage--><!--tab_title:UNKNOWN-->\n[vb " . $id[1] . "]";
else if (preg_match('#http://www.megavideo.com/v/(.+)[&"\']#si', $match, $id))
$edata[] = "<!--nextpage--><!--tab_title:MEGAVIDEO-->\n[megavideo " . $id[1] . "]";
}
if (isset($edata[0])) {
$embeds = implode("\n", ($edata));
print $embeds;
}
}
?>
这只输出:
[megavideo P5X0UOA267fb79acd04cdb29a057c3fa0066573a1]
每个其他嵌入的玩家都有匹配项。 flashvars 区域中的任何内容都不会。这可能不是真正的原因。 ..而像大型视频嵌入这样的东西却可以。我感谢任何帮助,谢谢!
This is the heart of my plugin that I've been painstakingly developing. Though I've ran into some trouble with the patterns..
<?php
$s = '
<embed type="application/x-shockwave-flash" id="single2" name="single2" src="http://api.realitylapse.com/player.swf" allowscriptaccess="always" allowfullscreen="true" wmode="transparent" flashvars="file=http://cerium.realitylapse.com/stream/bea352a230ebd36b52dc27d874f41f5a/4e3c5eca/default/xxxxx/xxxxx-lq.mp4&plugins=ltas<as.cc=inhldvymihzxqln&provider=http" height="424" width="659">
<embed type="application/x-shockwave-flash" src="http://www.xxxxx.com/player9397/player.swf?" quality="high" allowfullscreen="true" allowscriptaccess="always" wmode="opaque" flashvars="provider=http&file=http://www.xxxxx.com/player9397/vb.php?id=TT175YivmF4y&type=video&backcolor=111111&frontcolor=cccccc&lightcolor=DE4949&stretching=fill" height="420" width="99%">
<embed src="http://www.megavideo.com/v/P5X0UOA267fb79acd04cdb29a057c3fa0066573a1" type="application/x-shockwave-flash" allowfullscreen="true" width="100%" height="438">
';
$patterns = array();
$patterns[] = '<embed[^>]+src=["\'](.+?)["\']';
$patterns[] = '<embed[^>]+data=["\'](.+?)["\']';
$patterns[] = '<embed[^>]+flashvars="(.+?)["\']'; //Possible problem..
$patterns[] = '<embed[^>]+file=(.+?)[&]'; //
$patterns[] = '<iframe[^>]+src=["\'](.+?)["\']';
$patterns[] = '<iframe[^>]+data=["\'](.+?)["\']';
$patterns[] = '<object[^>]+src=["\'](.+?)["\']';
$patterns[] = '<object[^>]+data=["\'](.+?)["\']';
$patterns[] = '<video[^>]+src=["\'](.+?)["\']';
$patterns[] = '<video[^>]+data=["\'](.+?)["\']';
$patterns[] = '<video[^>]+file=(.+?)[&]';
$patterns = "#(?:" . implode("|", $patterns) . ")#si";
preg_match_all($patterns, ($s), $m);
if (!empty($m[0]))
{
$edata = array();
foreach($m[0] as $match)
{
//Embeds:
if (preg_match('#realitylapse.com/stream/(.+?)[&,"\']#si', $match, $id))
$edata[] = "<!--nextpage--><!--tab_title:CERIUM-->\n[cerium " . $id[1] . "]";
else if (preg_match('#http&file=http://www.xxxx.com/player9397/vb.php?id=(.+?)[&,"\']#si', $match, $id))
$edata[] = "<!--nextpage--><!--tab_title:UNKNOWN-->\n[vb " . $id[1] . "]";
else if (preg_match('#http://www.megavideo.com/v/(.+)[&"\']#si', $match, $id))
$edata[] = "<!--nextpage--><!--tab_title:MEGAVIDEO-->\n[megavideo " . $id[1] . "]";
}
if (isset($edata[0])) {
$embeds = implode("\n", ($edata));
print $embeds;
}
}
?>
This outputs only:
[megavideo P5X0UOA267fb79acd04cdb29a057c3fa0066573a1]
Every other player embed I have matches. Anything that is in the flashvars area does not. That may not be the real reason. ..Whereas something like the megavideo embed does. I appreciate any help, thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议使用 html 解析器,而不是使用 regexp,例如:
http://simplehtmldom.sourceforge.net/
然后您可以轻松读取元素属性。
Instead of using regexp, I would recommend using an html parser like:
http://simplehtmldom.sourceforge.net/
You can then easily read an elements attributes.