johnsmith153 Posted September 1, 2012 Share Posted September 1, 2012 I have this: if( preg_match('/[A-Z]/', $string) ) { ...but I want to exclude a set value so if the value THISVALUE is the only capitalised value then the if statement should return false. So, if THIS VALUE exists as well as other capitals, then it should return true, but fail if all are lower case (or if THIS VALUE is the only capitalised value). I can't think how to do this. Quote Link to comment https://forums.phpfreaks.com/topic/267895-simple-command/ Share on other sites More sharing options...
xyph Posted September 1, 2012 Share Posted September 1, 2012 What? Quote Link to comment https://forums.phpfreaks.com/topic/267895-simple-command/#findComment-1374526 Share on other sites More sharing options...
johnsmith153 Posted September 1, 2012 Author Share Posted September 1, 2012 (1) start with: if( preg_match('/[A-Z]/', $string) ) { (2) ...but I want to exclude a set value so if $string contains a set value (e.g "THISVALUE") as the only capitalised value, then the IF statement should return false (and not true as it would now). (3) However, if it contains the set value (e.g. "THISVALUE") and also contains other capital letters, then it should return true. Quote Link to comment https://forums.phpfreaks.com/topic/267895-simple-command/#findComment-1374541 Share on other sites More sharing options...
Christian F. Posted September 1, 2012 Share Posted September 1, 2012 Easiest would be to simply run an str_replace () on the string, before checking for capital letters. You should also work a bit on your terminology, as your question is quite ambiguous and thus confusing. Is "this value" a string, just a character, or something else? You're testing for a single capitalized character, but using "this value" as if it was a string of multiple characters. Some actual and specific examples would be a lot better, as we can see exactly what you need. Quote Link to comment https://forums.phpfreaks.com/topic/267895-simple-command/#findComment-1374546 Share on other sites More sharing options...
ignace Posted September 1, 2012 Share Posted September 1, 2012 Something like this? // so if the value THISVALUE is the only capitalised value then the if statement should return false // .. but fail if all are lower case (or if THIS VALUE is the only capitalised value). $test = false; if (preg_match_all('!([A-Z]+)!', $string, $matches)) { if (count(array_keys($matches[1], 'THISVALUE', true)) === 1 && count($matches[1]) > 1) { $test = true; // So, if THIS VALUE exists as well as other capitals, then it should return true } } if ($test) { // THISVALUE existed more than once } Quote Link to comment https://forums.phpfreaks.com/topic/267895-simple-command/#findComment-1374547 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.