Shaun Posted December 21, 2007 Share Posted December 21, 2007 Could someone please have a look over the following code and help to as why it is not functioning correctly. <? $str = "some text here \"REC UNREAD\" some more text here"; if(preg_match('/REC READ/', '$str') == "TRUE"){ $status = "read"; } elseif(preg_match('/REC UNREAD/', '$str') == "TRUE"){ $status = "unread"; } echo $status; ?> for some reason it wont echo out the status correctly. basically it pulls either REC READ or REC UNREAD from the string, and then echo's out whether it is read or un read.. I hope you can understand. please help me as this is starting to bug me. warm regards and thank you so much for the help. Quote Link to comment Share on other sites More sharing options...
mbeals Posted December 21, 2007 Share Posted December 21, 2007 To use your syntax preg_match('/REC\sREAD/', $str) != 0 lose the quotes and add \s. Preg_match only returns 'false' if there is an error, otherwise it returns the number of times there is a match. if you wanted to get fancy, you could even do: preg_match('/\"REC\s(\w+)\"/', $str, $status); echo $status[1]; I think it's status[1], you might want to print_r($status) to get check the index though. or if(substr_count($str, 'REC READ') != 0){$status = "read";} if(substr_count($str, 'REC UNREAD') != 0){$status = "unread";} string functions are generally faster then preg functions, and should be used when you don't need the power or flexibility of a preg function Quote Link to comment Share on other sites More sharing options...
Shaun Posted December 21, 2007 Author Share Posted December 21, 2007 ok, managed to fix this. Quote Link to comment 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.