Jump to content

Using preg_match to check for two values


Guardian-Mage

Recommended Posts

The way I have designed my program, and the fact that using preg match was the easiest solution. I am writing an interpreter, and I have an array of datatypes. It contains the name (boolean for example), and the regular expression that is used to verify the data. Thanks for the solution, it works well.

Link to comment
Share on other sites

if you are just wanting to test for equality and not actually capture a match, you don't need the 3rd parameter, and you can specify it as a non-captured group and also you don't need the 3rd argument.  Not a big deal; just an fyi

 

if (preg_match("/^(?:true|false)$/",$str)) {
   // matches, do something
}

 

Also, I know you said you wanted to use preg_match, but using 2 stripos conditions (one for each alternative) would be a lot faster.

Link to comment
Share on other sites

@ Guardian-Mage, while preg_match is certainly an easy way.. as CV mentioned, there are other (and faster alternatives).

Yet another alternative could involve say a switch statement (not going for pure speed here [and I didn't bother timing this method], but just illustrating another alternative):

$str = 'TRUE';
$checkStr = strtolower($str); // just to make everything easier to check...
switch($checkStr){
case 'true': echo 'String = \'true\''; break;
case 'false': echo 'String = \'false\''; break;
default: echo 'String is neither \'true\' nor \'false\'';
}

 

I would be hard pressed to believe that it MUST be preg... (although I have nothing against preg at all)

 

if (preg_match("/^(?:true|false)$/",$str)) {
   // matches, do something
}

 

Granted, if one went this route, I would add the i modifier and the third preg parameter (say $match) and within the if statement, check what $match[0] is equal to and go ahead accordingly... otherwise, which ever $str is set to, the outcome is the same, given the snippet provided. EDIT - unless of course, you mean further checking the string afterwards.. I just got the sense of an identical outcome...but now realize you probably meant further string checking with // matches, do something

Link to comment
Share on other sites

No, I mean, if you don't care whether it is 'true' or 'false', only that it is 'true' or 'false' (as opposed to some other arbitrary thing), then there's no need for the 3rd argument.  You know, like when you want to check if something is a number but don't care what the number actually is.  It seems that he is checking the 'value' to determine whether it's a boolean type.  He doesn't actually care whether it's true or false, just that it is a boolean type.  Therefore, the 3rd argument is not needed.  At least, that's what I interpreted.

 

I guess I would probably throw in the i modifier just to be safe. 

Link to comment
Share on other sites

@ Crayon Violent

 

You got what I was doing dead on. I don't care what the value ACTUALLY is, only that it is one of the given values. I use strtolower already, and I don't care if it is TRUE, true, TRuE, or tRuE. As long as it is true or false, another part of my program looks at the actual value.

 

@ nrg_alpha

 

It probably doesn't have to use preg_match, but this is how I set my program up. I have an array similar to this:

$this->CONFIGURATION_TYPES = array();

$this->CONFIGURATION_TYPES[0] = array("boolean","/^(true|false|0|1)$/");
$this->CONFIGURATION_TYPES[1] = array("integer","/^[0-9]$/");
//More code here

 

Which makes the code easy to extend. Hopefully you get the idea, I left a lot of code out :D

Link to comment
Share on other sites

@ nrg_alpha

 

It probably doesn't have to use preg_match, but this is how I set my program up. I have an array similar to this:

$this->CONFIGURATION_TYPES = array();

$this->CONFIGURATION_TYPES[0] = array("boolean","/^(true|false|0|1)$/");
$this->CONFIGURATION_TYPES[1] = array("integer","/^[0-9]$/");
//More code here

 

Which makes the code easy to extend. Hopefully you get the idea, I left a lot of code out :D

 

I understand... just note that when possible, use character classes instead (and might as well use the i modifier to match true, TRUE, True, etc..... (and as CV mentioned... make things non capture if you have no need to capture... Less burden on the regex engine...)

 

$this->CONFIGURATION_TYPES[0] = array("boolean","/^(?:true|false|[01])$/i");

 

EDIT - The order of detection within the alternation also has an impact on things... so perhaps:

"/^(?:[01]|true|false)$/i".. but I suppose this largely depends on the tendency of what the string will be.. but the idea is that a zero or a one is faster to check.. so get that out of the way first.. if the string does happen to be one of those, the rest of the alternation is over already.. if not, then proceed to check if it is true or false..

 

Link to comment
Share on other sites

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.