Preg_match resets an empty array

Given that I have a url https://website.com/test/demo/success.php?token=-abc123-, I want to get the value abc123. Somehow I find two blank lines on preg_match.

$url = 'https://website.com/test/demo/success.php?token=-abc123-';
preg_match('-(.*?)-', $url, $match);
var_dump($match);

Output: array(2) { [0]=> string(0) "" [1]=> string(0) "" }

What am I doing wrong here?

+3
source share
1 answer

You need to use the regex separator:

preg_match('/-(.*?)-/', $url, $match);
var_dump($match);

OR better:

preg_match('/-([^-]*)-/', $url, $match);
var_dump($match);
+4
source

All Articles