Lumio Posted January 3, 2009 Share Posted January 3, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/139324-the-following-word-must-not-be-in-a-string/ Share on other sites More sharing options...
.josh Posted January 3, 2009 Share Posted January 3, 2009 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"? Quote Link to comment https://forums.phpfreaks.com/topic/139324-the-following-word-must-not-be-in-a-string/#findComment-728705 Share on other sites More sharing options...
Lumio Posted January 3, 2009 Author Share Posted January 3, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/139324-the-following-word-must-not-be-in-a-string/#findComment-728716 Share on other sites More sharing options...
.josh Posted January 3, 2009 Share Posted January 3, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/139324-the-following-word-must-not-be-in-a-string/#findComment-728737 Share on other sites More sharing options...
Lumio Posted January 3, 2009 Author Share Posted January 3, 2009 No, they are the only one. Without any other text. Quote Link to comment https://forums.phpfreaks.com/topic/139324-the-following-word-must-not-be-in-a-string/#findComment-728748 Share on other sites More sharing options...
.josh Posted January 3, 2009 Share Posted January 3, 2009 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 } Quote Link to comment https://forums.phpfreaks.com/topic/139324-the-following-word-must-not-be-in-a-string/#findComment-728757 Share on other sites More sharing options...
Lumio Posted January 3, 2009 Author Share Posted January 3, 2009 is that quicker? but what if I want to say everythink but not PLAIN? Quote Link to comment https://forums.phpfreaks.com/topic/139324-the-following-word-must-not-be-in-a-string/#findComment-728772 Share on other sites More sharing options...
.josh Posted January 3, 2009 Share Posted January 3, 2009 then just do if ($string != "PLAIN") { // string not plain, do something } else { // string is plain, do something } Yes, straight string comparison is faster than regex. Quote Link to comment https://forums.phpfreaks.com/topic/139324-the-following-word-must-not-be-in-a-string/#findComment-728790 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.