PHP: Check if Start of each word in a sentence is uppercase or any part of a word is UPPERCASE

When working with strings, occasionally we would want to check if any part of a word is uppercase or start of a word is an uppercase letter. This post provide a generic function that can be extended to match your needs.

For this post I will use the string below as my reference string

Now let’s define our function that precisely checks a word for an Uppercase regardless of its position

preg_match() returns 1 if the pattern matches given subject, 0 if it does not, or FALSE if an error occurred.

Now let’s modify above function just to check if the first character is an Uppercase letter

Now let’s utilise the function we wrote above

I hope that in someway will be helpful to you, If you come across better method, I would like to know that,

Regards

Jas

 

Comments

  1. Way to complicated.

    $words=explode(‘ ‘,$sentence);
    foreach( $words as $word ){
    if( ucfirst($word) !== $word ) ===> no capital
    }

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.