-2

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 :)

WWWWWWWWWP
  • 35
  • 1
  • 6

4 Answers4

1

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);

?>


And see a demo on ideone.com.
Jan
  • 42,290
  • 8
  • 54
  • 79
0

Use this regex: (?<=&session=)[\da-zA-Z]*

You'll also need to escape special html characters in PHP code.

0
(?: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

Israel Unterman
  • 13,158
  • 4
  • 28
  • 35
0

Here is your regexp.

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