Jump to content

Simple command


johnsmith153

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/267895-simple-command/
Share on other sites

(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.

Link to comment
https://forums.phpfreaks.com/topic/267895-simple-command/#findComment-1374541
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/267895-simple-command/#findComment-1374546
Share on other sites

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
}

Link to comment
https://forums.phpfreaks.com/topic/267895-simple-command/#findComment-1374547
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.