Guardian-Mage Posted January 26, 2009 Share Posted January 26, 2009 How can I use preg match to see if a string is EQUAL to (Not contains) the string "false" or "true"? I have to use preg match, so that is the only solution I am looking for Thanks Quote Link to comment https://forums.phpfreaks.com/topic/142469-using-preg_match-to-check-for-two-values/ Share on other sites More sharing options...
zq29 Posted January 26, 2009 Share Posted January 26, 2009 How are you restricted in that you can only use preg_match? <?php preg_match("/^(true|false)$/",$str,$matches); print_r($matches); ?> Quote Link to comment https://forums.phpfreaks.com/topic/142469-using-preg_match-to-check-for-two-values/#findComment-746502 Share on other sites More sharing options...
Guardian-Mage Posted January 26, 2009 Author Share Posted January 26, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/142469-using-preg_match-to-check-for-two-values/#findComment-746532 Share on other sites More sharing options...
.josh Posted January 26, 2009 Share Posted January 26, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/142469-using-preg_match-to-check-for-two-values/#findComment-746549 Share on other sites More sharing options...
nrg_alpha Posted January 26, 2009 Share Posted January 26, 2009 @ 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 Quote Link to comment https://forums.phpfreaks.com/topic/142469-using-preg_match-to-check-for-two-values/#findComment-746632 Share on other sites More sharing options...
.josh Posted January 26, 2009 Share Posted January 26, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/142469-using-preg_match-to-check-for-two-values/#findComment-746688 Share on other sites More sharing options...
nrg_alpha Posted January 26, 2009 Share Posted January 26, 2009 Yeah, I realize now what approach you have taken.. I was under a different impression. Quote Link to comment https://forums.phpfreaks.com/topic/142469-using-preg_match-to-check-for-two-values/#findComment-746691 Share on other sites More sharing options...
Guardian-Mage Posted January 26, 2009 Author Share Posted January 26, 2009 @ 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 Quote Link to comment https://forums.phpfreaks.com/topic/142469-using-preg_match-to-check-for-two-values/#findComment-746765 Share on other sites More sharing options...
nrg_alpha Posted January 26, 2009 Share Posted January 26, 2009 @ 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 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.. Quote Link to comment https://forums.phpfreaks.com/topic/142469-using-preg_match-to-check-for-two-values/#findComment-746780 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.