Matching letters regardless of letter case (e.g. WORD = Word = word)

Imagine a database value Elmer_Fudd, which displays a page at MySite/People/Elmer_Fudd

Now imagine someone typing in the URL with a space instead of an underscore and/or using lower case instead of capitals:

MySite/People/elmer fudd

I’d like such URL’s to default to the value in my database, where $URL = a value in a database and $MyURL = a page URL (which may or may not = $URL).

I can handle the underscore like this:

$URL_Approximate = str_replace(‘_’, ’ ', $URL);


switch($MyURL)
{
 case $URL:
 // Display page
 break;
 case $URL_Approximate:
 // 301 Redirect
 break;
 default:
 break;
}

I don’t know if I got the code exactly right, but I think that illustrates what I’m trying to do. What I’m trying to figure out is how to handle the case. Suppose someone types in the right letters, with or without the underscore, but with the wrong case - e.g. “elmer_fudd”, “elmer fudd”, “ELMER FUDD”, etc. - how do I modify the script so those versions are recognized as a match for the database value, Elmer_Fudd?

I think you’re overlooking one simple fact: querying a database (presumably MySQL) is a case-insensitive operation (unless specifically made case-sensitive by the BINARY keyword). If you can make sure the the user input has the correct formatting (i.e. uses underscores for word separation - which it seems like you have already done), then you should be fine.

If you’re comparing strings within PHP:

You can use the strtolower() function to convert everything to lower case letters, then do the string comparison.

http://us2.php.net/manual/en/function.strtolower.php

Or, you can use strcasecmp(), which is a case insensitive string comparison.

http://www.php.net/manual/en/function.strcasecmp.php

If you’re retrieving something from a database, the WHERE clauses are case insensitive, as tpunt explained, so you wouldn’t need to use either of the PHP functions I mentioned above.

I can handle the underscore like this:

$URL_Approximate = str_replace(‘_’, ’ ', $URL);

Please check that the actual $URL contains a space and not “%20”



 $URL_Approximate = str_replace( '%20', "_", $URL);


My preference is to use hyphens instead of underscores which I believe are more SEO friendly.

Thanks for all the tips. Wow, I didn’t even know database queries were insensitive (except for the “filler” stuff, like, select, where and as).

As for underscores versus hyphens, I had that big debate with myself long ago and settled on underscores for some reason. It would be pretty hard to change now; I’d have to change over 50,000 links. But now that you mention it, I think the major blogs do use hyphens.

It’s amazing how many details one has to deal with in web design; making the perfect web page is virtually impossible. :wink:

The web is changing constantly. It’s impossible to keep up with everything these days. But, what tends to help is to read and join in on discussions, and read web development blogs. I tend to hit a handful of blogs about once a month or so. That usually keeps me reasonably up-to-date on stuff in general. What also keeps you involved is constantly dabbling and working on projects and encountering problems on those projects or raising the question “is there a better way to do this?”