Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex在meta中查找文本_Regex - Fatal编程技术网

Regex在meta中查找文本

Regex在meta中查找文本,regex,Regex,使用PHP的正则表达式在变量“URL”中查找“session”的内容是什么: $var = "<meta http-equiv='refresh' content='0; URL=/game/index.php?page=overview&session=9efae87dd67&lgn=1'>" $var=“” 谢谢:)使用这个正则表达式:(? 虽然我发现使用括号更容易: session=(\w+) 在最后一种情况下,您需要访问匹配号1,这是您的regexp p

使用PHP的正则表达式在变量“URL”中查找“session”的内容是什么:

$var = "<meta http-equiv='refresh' content='0; URL=/game/index.php?page=overview&session=9efae87dd67&lgn=1'>"
$var=“”
谢谢:)

使用这个正则表达式:
(?
虽然我发现使用括号更容易:

session=(\w+)
在最后一种情况下,您需要访问匹配号1,这是您的regexp

preg_match("/session=(\\w+)/", $var, $session);
echo $session[1]; // => "9efae87dd67"

实际上,将
DOM
解析器与正则表达式组合使用,如下所示:

<?php

$data = <<<DATA
<meta http-equiv='refresh' content='0; URL=/game/index.php?page=overview&session=9efae87dd67&lgn=1'>
<body/>
DATA;

$previous_value = libxml_use_internal_errors(TRUE);

$dom = new DOMDocument();
$dom->loadHTML($data);

$xpath = new DOMXPath($dom);

$meta = $xpath->query("//meta")->item(0);

$regex = '~session=\K[^&]+~';
preg_match($regex, $meta->getAttribute("content"), $session);
echo $session[0];

libxml_clear_errors();
libxml_use_internal_errors($previous_value);

?>
loadHTML($data);
$xpath=newdomxpath($dom);
$meta=$xpath->query(“//meta”)->项(0);
$regex='~session=\K[^&]+~';
preg_match($regex,$meta->getAttribute(“内容”),$session);
echo$会话[0];
libxml_clear_errors();
libxml_使用_内部_错误($previous_值);
?>


请参阅。

(?这不起作用:$test=“”;$pattern=”(?)?
<?php

$data = <<<DATA
<meta http-equiv='refresh' content='0; URL=/game/index.php?page=overview&session=9efae87dd67&lgn=1'>
<body/>
DATA;

$previous_value = libxml_use_internal_errors(TRUE);

$dom = new DOMDocument();
$dom->loadHTML($data);

$xpath = new DOMXPath($dom);

$meta = $xpath->query("//meta")->item(0);

$regex = '~session=\K[^&]+~';
preg_match($regex, $meta->getAttribute("content"), $session);
echo $session[0];

libxml_clear_errors();
libxml_use_internal_errors($previous_value);

?>