How to detect if string contains 1 uppercase letter in PHP

Couldn't find a function for this. I'm assuming I need to use regex?

I'm trying to do html redirects in php in cases where the url contains at least 1 upper case letter.

example: http://www.domain.com/Michael_Jordan needs to be redirected to http://www.domain.com/michael_jordan - only problem is I can't seem to find a script to detect if at least 1 capital letter exists.

Answers


Some regular expression should be able to the work, you can use preg_match and [A-Z]

if(preg_match('/[A-Z]/', $domain)){
 // There is one upper
}

if (strtolower($url) != $url){
  //etc...

You can also try this

if (!ctype_lower($string)) {
    // there is at least une uppercase character
}

not sure if this is more efficient than the other two methods proposed.


preg_match_all('%\p{Lu}%usD', 'aA,éÁ,eE,éÉ,iI,íÍ,oO,óÓ,öÖ,őŐ,uU,úÚ,üÜ,űŰ', $m);
echo '<pre>';
var_dump($m);
echo '</pre>';

Tested with hungarian utf-8 characters, [A-Z] is for latin1 only.


Here is a simpler eg:

$mydir = "C:\Users\John" ;

print preg_match('/^[A-Z]:\.*/', $mydir, $match )."\n" ;
print $match[0]. " preg match \n" ;

Produces:

1
C: preg match

This suggests that the parens are not necessary --for one match, at least

Look at this to be more specific for your application: PHP to SEARCH the Upper+Lower Case mixed Words in the strings?


Need Your Help

php sprintf with array

php arrays printf

I have an array witch match string placeholders as follow:

Dividing a plane of points into two equal halves

algorithm math geometry computational-geometry

Given a 2 dimensional plane in which there are n points. I need to generate the equation of a line that divides the plane such that there are n/2 points on one side and n/2 points on the other. (b...