chris chris - 1 month ago 11
PHP Question

Image URL preg_match failing, can't figure out why

So I tested this pattern at regex101, and it works, but when I load it up into PHP I keep getting false, can someone please explain to me why?

preg_match('/^(https?|ftp):\/\/.*(jpeg|png|gif|bmp|jpg)/gi', $_POST['damageImage'])


the
$_POST
would be equal to
http://domain.com/hello/random-230x190.png
for example sake.

I have also tried this with
preg_match_all()
still recieving a false. All I want to do is a simple if-else statement and I can't get past this simple thing. Its got me stumped, what is the overall pitfall I am tripping over here?

Answer

Remove g modifier

$url = 'http://domain.com/hello/random-230x190.png';
preg_match('/^(https?|ftp):\/\/.*(jpeg|png|gif|bmp|jpg)/i', $url, $match);

var_dump($match);
Comments