PHP 8.4.24 Released!

Voting

: one minus one?
(Example: nine)

The Note You're Voting On

Greg S
17 years ago
I did the same thing as Catalin, but for French names.

Here's what I'm doing :

For each word (not considering the single-quote as a word boundary character) :
- Put the word in lower case
- If the word is "de", return, else, put the first letter in upper-case
- See if the second character of the word is a single-quote
- Yes ? Put the next char in upper case
- And if the char before the single quote is D, put it back to lower case (-> d)

This complies with the French rules for capitalization in names.

Sample results :
-d'Afoo Bar
-de Foo Bar
-O'Foo Bar
-Foo'bar

<?php
function my_ucwords($s) {
        $s = preg_replace_callback("/(\b[\w|']+\b)/s", fixcase_callback, $s);
        
        return $s;         
        
    }
    
    function fixcase_callback($word) {

        $word = $word[1];
        
        $word = strtolower($word);
        
        if($word == "de")
            return $word;
        
        $word = ucfirst($word);
        
        if(substr($word,1,1) == "'") {
            if(substr($word,0,1) == "D") {
                $word = strtolower($word);
            }
            $next = substr($word,2,1);
            $next = strtoupper($next);
            $word = substr_replace($word, $next, 2, 1);
        }
        
        return $word;
    }
?>

<< Back to user notes page

To Top