What is the regex with PHP to find the content of "session" in "URL" in this variable :
$var = "<meta http-equiv='refresh' content='0; URL=/game/index.php?page=overview&session=9efae87dd67&lgn=1'>"
Thanks :)
What is the regex with PHP to find the content of "session" in "URL" in this variable :
$var = "<meta http-equiv='refresh' content='0; URL=/game/index.php?page=overview&session=9efae87dd67&lgn=1'>"
Thanks :)
Really, use a DOM
parser combined with regular expressions, like so:
<?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);
?>
Use this regex: (?<=&session=)[\da-zA-Z]*
You'll also need to escape special html characters in PHP code.
(?:session=)\w+
Although I find it easier on the mind to use capturing parenthesis:
session=(\w+)
In the last case you will need to access match no. 1
Here is your regexp.
preg_match("/session=(\\w+)/", $var, $session);
echo $session[1]; // => "9efae87dd67"