Jump to content

[Solved] test if string has upper case letters


damiancds

Recommended Posts

I've been looking around, and I can't find what I need. Recently, I made it easier to navigate by removing all case sensitivities from my urls. The problem is that I still get some misdirected attempts still going to the older case sensitive urls.On my 404 page, I'm trying to take the url they entered and give them back an all lower case url, Which is done, the thing is, I only want to give it out when there is upper case letters in the url. So, I'm basically asking if there is any way of testing a string to see if it has at least one upper case letter, then if it does print the URL in lower case. The only thing I came close to is ctype_upper() but, that only returns true if ALL of them are upper case, and I want to test if at least one character is upper case.If anyone has any quick ideas, let me know. Right now I'm going to start writing my own function that'll run through the string and test if each letter is in the ascii value of an upper case letter (i think is somewhere from 66 and up or so)thanks,

Link to comment
Share on other sites

You could compare the URL the user entered to an all lowercase version using strcmp() (http://w3schools.com/php/func_string_strcmp.asp).Use strtolower() to convert the URL to lowercase and assign it to a new variable:$tmpURL = strtolower($url);Then compare $tmpURL to $url with strcmp() and if they're different then $url has uppercase letters

Link to comment
Share on other sites

Yeah, I went back and looked, because I couldn't figure out how to get the character at a certain position and noticed the strcmp. I thought it didn't deal with case sensitive but then i saw it would return a negative. After i put it in it works great.thanks,

Link to comment
Share on other sites

Yeah, I went back and looked, because I couldn't figure out how to get the character at a certain position...
FWIW, you can access a character in a string the same way you access a value in an array.$string = "This is a string";echo $string[3]; //Prints 's' as in the 's' in 'This'
Link to comment
Share on other sites

Archived

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

×
×
  • Create New...