top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

preg_match in PHP?

+2 votes
520 views

Is this correct syntax?

case preg_match("/^[-1|delete|hide|save]/", $MassOption) !== 1 :

I am needing to test against -1 (negative 1) and the words delete, hide, and save. $MassOption should not equal those.

posted Sep 22, 2015 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

No, in regular expressions, the [ ] are for matching individual characters, not strings. You could instead perform a standard match in () and just negate the result of preg_match() with a !.

answer Sep 22, 2015 by Honey
I did finally come across the () however the ! eluded me. Wherein do I place the ! mark? Like so?

preg_match("/^![-1|delete|hide|save]/", $MassOption) !== false

or just?

preg_match("/^![-1|delete|hide|save]/", $MassOption)
You wouldn't place it in your Regex, you use it to negate your call to preg_match:

!preg_match()

And get rid of the square brackets, they are for character matches, not string matches.
Similar Questions
0 votes

recently I have made a form code and attached modules to it. but soon after I have reached an error saying Notice: Undefined index: module in F:\website\webpage\gate.php on line 68

here is the line that was mentioned

   switch ($_GET["module"]) {
    case "settings":
      Include('settings.php');
      break;
    default:
      break;
   }

I am also mentioning the code in the that I showed up ib the link

<a class="nav-link" href="gate.php?module=settings"> Settings </a>

please rectify this error for me and sorry for any in trouble

...