Jump to content

[Solved] test if string has upper case letters


damiancds
 Share

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,

Edited by damiancds
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

Edited by jkloth
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,

Edited by damiancds
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' Edited by jkloth
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...