我需要PHP中的regexp来查找URL中的http-equiv="refresh“元标记。我需要的是实际的网址来遵循。现在,据我所知,有两种有效的方法来使用这个meta标签:
content="0; url=urlhere" http-equiv="refresh" />和
http-equiv="refresh" content="0; url=urlhere"/>谢谢!
发布于 2009-09-03 00:00:54
迪玛
试试这个:
<?
preg_match('|content="\d+;url=(.*?)"|i', '<META HTTP-EQUIV="Refresh" CONTENT="5;URL=http://www.stackoverflow.com">', $res1);
preg_match('|content="\d+;url=(.*?)"|i', '<META CONTENT="5;URL=http://www.stackoverflow.com" HTTP-EQUIV="Refresh">', $res2);
echo "<pre>";
var_dump($res1);
var_dump($res2);
echo "</pre>";
?>输出:
array(2) {
[0]=>
string(44) "CONTENT="5;URL=http://www.stackoverflow.com""
[1]=>
string(28) "http://www.stackoverflow.com"
}
array(2) {
[0]=>
string(44) "CONTENT="5;URL=http://www.stackoverflow.com""
[1]=>
string(28) "http://www.stackoverflow.com"
}请记住,您必须处理空白(内容属性内部、标记之间、http-equiv属性内部等),例如:
<META HTTP-EQUIV="Refresh" CONTENT=" 5 ; URL=http://www.stackoverflow.com ">下面的代码片段处理这种情况:
<?
preg_match('|content="\s*\d+\s*;\s*url=(.*?)\s*"|i', '<META HTTP-EQUIV="Refresh" CONTENT=" 5 ; URL=http://www.stackoverflow.com ">', $res3);
echo "<pre>";
var_dump($res3);
echo "</pre>";
?>输出:
array(2) {
[0]=>
string(48) "CONTENT=" 5 ; URL=http://www.stackoverflow.com ""
[1]=>
string(28) "http://www.stackoverflow.com"
}最后,如果这还不够,您可以在content属性的每一侧检查http-equiv="refresh“(始终考虑空格),如下所示:
<?
preg_match('|(?:http-equiv="refresh".*?)?content="\d+;url=(.*?)"(?:.*?http-equiv="refresh")?|i', '<META HTTP-EQUIV="Refresh" CONTENT="5;URL=http://www.stackoverflow.com">', $res4);
preg_match('|(?:http-equiv="refresh".*?)?content="\d+;url=(.*?)"(?:.*?http-equiv="refresh")?|i', '<META CONTENT="5;URL=http://www.stackoverflow.com" HTTP-EQUIV="Refresh">', $res5);
echo "<pre>";
var_dump($res4);
var_dump($res5);
echo "</pre>";
?>输出:
array(2) {
[0]=>
string(44) "CONTENT="5;URL=http://www.stackoverflow.com""
[1]=>
string(32) "http://www.stackoverflow.com"
}
array(2) {
[0]=>
string(65) "CONTENT="5;URL=http://www.stackoverflow.com" HTTP-EQUIV="Refresh""
[1]=>
string(32) "http://www.stackoverflow.com"
}您可以使用相同的方法。添加对考虑部件的支持。
此外,请记住始终使用i选项运行正则表达式,以启用不区分大小写的匹配。
发布于 2009-09-02 20:58:47
http-equiv\W*refresh.+?url\W+?["'](.+?)["']尝试:
if (preg_match('/meta.+?http-equiv\W+?refresh/i', $x)) {
preg_match('/content.+?url\W+?["\'](.+?)["\']/i', $x, $matches);
print_r($matches);
}https://stackoverflow.com/questions/1370025
复制相似问题