26
votes

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.

5
@Bob: I don't see the point of doing as same as your example since DNS name are case insensitive.RageZ
It's something I was asked to do in order to improve seo - changing url site structure - past URLs had capitalized letters - so we're trying to preserve page strength from those urls while transitioning over to the new ones.Bob Cavezza
Does Google or any search engine actually take case into account for the domain?deceze♦
Not so sure about domain - but we're more worried about the trailing text in the pages. For example, many of the urls are basketball player names, and the original links were all with First_Last format, that we're changing to first_last and apparently there is a difference there - not 100% about the domains. wisegeek.com/are-urls-case-sensitive.htmBob Cavezza
Makes sense for the local parts of the URL (everything after the domain), which are indeed case sensitive. If you want to rewrite those as well, the question makes sense. If it was only for the domain, I wouldn't worry about it.deceze♦

5 Answers

53
votes

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 at least one upper
}
32
votes
if (strtolower($url) != $url){
  //etc...
5
votes

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.

2
votes
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.

0
votes

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?