Jump to content

Recommended Posts

Hello,

I have a little problem with regular expressions.

 

I got the following string:

$str = "PLAIN";

 

So what I want to do now is to say, that if the string is PLAIN, that it is not true, but with a regular expression.

 

I tried

<?php
$str = "PLAIN";
$r = "^PLAIN";

var_dump(preg_match("/($r)/", $str));

But it always gets 1 and not 0. Why?

You are only getting 1 or 0 because preg_match returns a true on successful match, false on no matches when you assert it in a condition like that (or assign it to a var).  Actual results would be put into the 3rd argument of preg_match

 

Beyond that....be more specific.  Are you wanting to dump out the contents of $string if it is not "PLAIN" or if it does not contain "PLAIN" in it somewhere? Or are you simply trying to find out whether it is or contains "PLAIN"?

Hi!

Thanks for your quick answere.

Well I do have more keywords like that.

For example K_IF or K_ELSE or K_ENDIF.

 

Let's say, I only want to let a condition get true, if it is not PLAIN. Okay, for that reason I can use a normal if-statement. But lets say I don't want to have lets say PLAIN and also not K_ENDIF. So I thought ^PLAIN|^K_ENDIF would do it, but no. It doesn't :)

 

I want to match the whole key.

Okay but are those "keywords" the only thing in the string like

 

"PLAIN"

"K_IF"

etc..

 

or are they just one piece of a string like

 

"PLAIN blahblahlbha"

"blah PLAIN blahblah"

 

And again, are you trying to retrieve something from somewhere or are you simply trying to find out whether it's true or not.

okay so then you can just do this:

 

$list = array('PLAIN','K_IF','K_ELSE','K_ENDIF');
if (!in_array($string, $list)) {
   // string is not one of those things, do something
} else {
  // string is one of those things, do something
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.