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?
You need to use regex delimiter:
preg_match('/-(.*?)-/', $url, $match);
var_dump($match);
OR better:
preg_match('/-([^-]*)-/', $url, $match);
var_dump($match);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With