Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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