Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

preg_match dumps empty array

Tags:

regex

php

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?

like image 505
Stan Avatar asked Mar 11 '26 08:03

Stan


1 Answers

You need to use regex delimiter:

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

OR better:

preg_match('/-([^-]*)-/', $url, $match);
var_dump($match);
like image 183
anubhava Avatar answered Mar 13 '26 23:03

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!