0

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?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
chris
  • 36,115
  • 52
  • 143
  • 252
  • 2
    Remove `g` modifier. `'/^(https?|ftp):\/\/.*(jpeg|png|gif|bmp|jpg)/i'`. You might also want to anchor it at the end with `$`: `'/^(https?|ftp):\/\/.*(jpeg|png|gif|bmp|jpg)$/i'` – Wiktor Stribiżew Oct 25 '16 at 06:01
  • Yea, that was it.. I actually tried a varient without it earlier and must have still had something off, and for whatever reason I tacked on that global check to see if that would resolve it at one point. Thanks! – chris Oct 25 '16 at 06:05
  • Possible duplicate of ["Unknown modifier 'g' in..." when using preg\_match in PHP?](http://stackoverflow.com/questions/3578671/unknown-modifier-g-in-when-using-preg-match-in-php) – Wiktor Stribiżew Oct 25 '16 at 06:08

1 Answers1

1

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);
Manh Nguyen
  • 930
  • 5
  • 12