1

URL で http-equiv="refresh" メタ タグを見つけるには、PHP で正規表現が必要です。必要なのは、従うべき実際の URL です。現在、私が知る限り、このメタ タグを使用する有効な方法は 2 つあります。

content="0; url=urlhere" http-equiv="refresh" />

http-equiv="refresh" content="0; url=urlhere"/>

ありがとう!

4

2 に答える 2

7

ディマ、

これを試して:

<?
  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"
}

最後に、それだけでは不十分な場合は、次のようにコンテンツ属性の両側で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-03T00:00:53.760 に答える
3
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);
}
于 2009-09-02T20:58:47.277 に答える