2009-09-02 11 views
0

J'ai besoin d'une expression rationnelle en PHP pour trouver la balise META-equiv = "refresh" dans une URL. Ce dont j'ai besoin, c'est l'URL à suivre. Maintenant, pour autant que je sais qu'il ya deux façons valables d'utiliser cette balise meta:Regex pour http-equiv = "refresh" meta tag

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

et

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

Merci!

+0

content =. + Url = ['"] (. +) Capturé dans \ 1 – Cleiton

+0

Comment l'utiliser avec preg_match? Même erreur: Le délimiteur ne doit pas être alphanumérique ou backslash –

Répondre

4
http-equiv\W*refresh.+?url\W+?["'](.+?)["'] 

Essayez:

if (preg_match('/meta.+?http-equiv\W+?refresh/i', $x)) { 
    preg_match('/content.+?url\W+?["\'](.+?)["\']/i', $x, $matches); 
    print_r($matches); 
} 
+0

me donne une erreur ... Délimiteur ne doit pas être alphanumérique ou antislash pour preg_match –

+0

Voyons le code – jmans

+0

preg_match ('http-equiv \ W * refresh. +? url \ W +? (. +?) \ "', file_get_contents ($ x), $ matches) ; –

3

Dima,

Essayez ceci:

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

Sortie:

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

Gardez à l'esprit que vous aurez à traiter avec des espaces blancs (à l'intérieur attribut contenu, entre les balises, à l'intérieur attribut http-equiv, etc.), tels que:

<META HTTP-EQUIV="Refresh" CONTENT=" 5 ; URL=http://www.stackoverflow.com "> 

L'extrait de code suivant gère ce cas:

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

sortie:

array(2) { 
    [0]=> 
    string(48) "CONTENT=" 5 ; URL=http://www.stackoverflow.com "" 
    [1]=> 
    string(28) "http://www.stackoverflow.com" 
} 

Enfin, si cela ne suffit pas, vous pouvez vérifier http-equiv = « refresh » de chaque côté de l'attribut de contenu (Takin toujours en compte les espaces blancs) comme t son:

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

Sortie:

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

Vous pouvez, en utilisant la même approche. ajouter un support pour prendre en compte les pièces.
De plus, souvenez-vous toujours d'exécuter des regex avec l'option i, pour activer la correspondance insensible à la casse.