Search code examples
phpstringnormalize

PHP: Normalize a string


I want to normalize (so canonicalize) a string into the normal form for names:

  1. First letter of the name is uppercase

The difficulty by this is now to follow this rule with second and third name.

My method:

    public function namilize($string)
{
    $strings = explode(' ', $string);
    foreach ($strings as $string) {
        $string = ucfirst(strtolower($string));
    }
    $string = implode(' ', $strings);

    return $string;
}

Somehow the

$string = ucfirst(strtolower($string));

fails.

What do I have to correct? Is there a better way?

Regards

EDIT:

Hi,

thank you all for all the comments and answers. I found another "modern" method:

public function namilize($string)
{
    $string = mb_convert_case($string, MB_CASE_TITLE, mb_detect_encoding($string));
}

When I now would additionally add some regex for Mc and O's than it would be complete :)


Solution

  • public function namilize($name) {
        $name = strtolower($name);
        $normalized = array();
    
        foreach (preg_split('/([^a-z])/', $name, NULL, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY) as $word) {
            if (preg_match('/^(mc)(.*)$/', $word, $matches)) {
                $word = $matches[1] . ucfirst($matches[2]);
            }
    
            $normalized[] = ucfirst($word);
        }
    
        return implode('', $normalized);
    }
    

    Note that this will work for names like John O'Brian, James McManus, etc. For other names with prefixes like McManus, simply add the prefix to the preg_match(). Obviously, this runs the possibility of false positives, but no method is going to be 100% foolproof.