1

With regular expressions, how do I match anything not having:

.php

exactly? I tried:

^[^\.php]+$

But this seems to be match not . or p or h or p.

Justin
  • 42,716
  • 77
  • 201
  • 296
  • Seems to be what you are looking for: http://stackoverflow.com/questions/5704061/php-reverse-preg-match – fabien Mar 25 '14 at 06:15
  • Humm, can't get it working. Tried `^(?!(\.php$))`. – Justin Mar 25 '14 at 06:24
  • ... which is not like in the example. That works: $filter = ".php$"; $text = "stuff.php3"; if (preg_match("/^(?:(?!" . $filter . ").)*$/i", $text)) { echo "yeahhh"; } – fabien Mar 25 '14 at 06:28

1 Answers1

2

Use negative lookahead in your regex to check whether there is any .php is available or not.

$text = 'some php text';
if(preg_match("/^(?!.*\.php)(.*)$/", $text, $m)){
    print_r($m[1]);
}

^ is the beginning of the string.

(?!.*\.php) is checking if there is no .php in upfront.

And (.*)$ is capturing everything till the end.

Sabuj Hassan
  • 38,281
  • 14
  • 75
  • 85