I use strtolower() to take name inputs on a form and keep them from being all caps or all lowercase.

The problem is the names that begin with Mc, Mac, De, Van, St., etc. Has anyone seen an 'intelligent' method to accomplish this? I could do it, but I'm sure the code would be way too bloated.

    How do you propose such a function should know whether or not to capitalize "macaroon" as opposed to "MacArthur"?

      NogDog wrote:

      How do you propose such a function should know whether or not to capitalize "macaroon" as opposed to "MacArthur"?

      Gosh, I hope some poor soul out there doesn't have the last name of "Macaroon". But, yes, that poses a problem. And, sometimes names that begin with "De" would not always be that such as "Devon" or "Dedman".

      Quite the problem.

        I think the best you'd get without some form of dictionary check would be something like this:

        $formattedName = ucwords(strtolower($unformattedName));
        

        Which would take, say, AaRoN DeBeeRS, to Aaron Debeers, which is better than nothing, although still not as good as Aaron DeBeers.

        (Would also take 'aaron debeers' to 'Aaron Debeers')

          Horizon88 wrote:

          I think the best you'd get without some form of dictionary check would be something like this:

          $formattedName = ucwords(strtolower($unformattedName));
          

          Which would take, say, AaRoN DeBeeRS, to Aaron Debeers, which is better than nothing, although still not as good as Aaron DeBeers.

          (Would also take 'aaron debeers' to 'Aaron Debeers')

          Which I'm already using. I was just hoping for a more elegant solution. But, names can be manually adjusted in this particular database to reflect it.

          Most people who have names with multiple caps usually do not enter all lower case or all upper case into web forms anyway, but there are a few select who have no pride in themselves whatsoever.

            I think I mis-read the original question to apply to general text entries, not specifically to name inputs. I would probably opt for ucwords() when outputting to a web page or such, and strtoupper() for things like mail labels. Another possibility would be to look for all lower-case or all upper-case, and assume the user typed it correctly if there is a mix:

            $name = trim($_POST['name']);
            if(!preg_match('/[A-Z]/', $name) or !preg_match('/[a-z]/', $name))
            {
               $name = ucwords(strtolower($name));
            }
            
              Write a Reply...