Stan
Stan

Reputation: 26501

preg_match dumps empty array

Given that I have url https://website.com/test/demo/success.php?token=-abc123- I want to get value abc123. Somehow I get two empty strings on my 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?

Upvotes: 3

Views: 111

Answers (1)

anubhava
anubhava

Reputation: 785058

You need to use regex delimiter:

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

OR better:

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

Upvotes: 4

Related Questions