2

Currently, I'm using a ucwords-related function to make capital letters after hyphens, dots and apostrophes:

function ucwordsMore ($str){
    $str = ucwords($str);
    $str = str_replace('- ','-',ucwords(str_replace('-','- ',$str)));  // hyphens
    $str = str_replace('. ','.',ucwords(str_replace('.','. ',$str)));  // dots
    $str = preg_replace("/\w[\w']*/e", "ucwords('\\0')", $str);        // apostrophes

    return $str;
}

It works fine to english letters. However, non-english letters are not recognized properly. For instance this text:

La dernière usine française d'accordéons reste à Tulle

is turned into this text:

La DernièRe Usine FrançAise D'accordéOns Reste à Tulle

But I need it to be:

La Dernière Usine Française D'Accordéons Reste À Tulle

Any ideas?

2
  • what are english letters really? It's the Latin alphabet. According to your example it seems that the problem lies with accented letters. I'd argue that the A of Accorde'ons is correct though, you can't expect php to know that a new word starts without a space before it, and the apostrophe isn't a good delimiter either as it doesn't ALWAYS indicate a word end/start.
    – Bazzz
    Mar 22, 2012 at 14:00
  • php is pretty much hosed for dealing with Unicode properly; sorry, but some things that are trivial and automatic in say Perl are simply impossible in php.
    – tchrist
    Mar 22, 2012 at 17:37

4 Answers 4

2

As @Jon mentioned, you need to use locale which implements relationships between upper/lower caseing that affects function calls that use that. Typically it is LC_CTYPE.

There are constants for numeric behavior, sorting, monetary and others too. Locale needs to be installed on your machine, or be available via plugins or modules, etc. Read up on that.

I don't know php locale at all so here is a sample in Perl that uses a regex approach different than yours. I couldn't figure out your solution so well, hopefully you can get some ideas from mine.

use locale;
use POSIX qw(locale_h);

setlocale(LC_CTYPE, "en_US");

$str = "La dernière usine française d'accordéons reste à Tulle";

$str =~ s/ (?:^|(?<=\s)|(?<=\w-)|(?<=\w\.)|(?<=\w\')) (\w) / uc($1) /xeg;

print "$str\n";

Output

La Dernière Usine Française D'Accordéons Reste À Tulle

Regex

Form is s///  find and replace

s/                  # Search

  (?:                  # Group
      ^                   # beginning of string
    | (?<=\s)             # or, lookbehind \s
    | (?<=\w-)            # or, lookbehind \w-
    | (?<=\w\.)           # or, lookbehind \w\.
    | (?<=\w\')           # or, lookbehind \w\'
  )                    # End group
  (\w)                 # Capture group 1, a single word char

/                   # Replace
  uc($1)               # Upercased word char from capt grp 1

/xeg;               # Modifiers x(expanded), e(eval), g(global)
3
  • Um, no. You just have to have your strings marked as Unicode strings. You never have to use locale to get correct casing behaviour, except perhaps for Turkic languages, which that won’t help anyway.
    – tchrist
    Mar 22, 2012 at 17:37
  • @tchrist - I asumed its not Unicode, thats why locale was used. Perl doesen't understand the concept of Unicode locales anyway, why coerce to Unicode if its not, and couldn't the regex performance degrade because of this? Internally it could be mixed 8-bit encoding with character semantics but I thought Unicode support overhead slows things down. And ah, I am not sure what level of unicode, PHP supports.
    – user557597
    Mar 22, 2012 at 18:42
  • What, you value speed over correctness then, do you? Always normalize legacy encodings to Unicode first. Anyway, Perl supports Unicode locales in the collation module. That’s not in regexes of course, or even casing. Casing is not supposed to be locale-based.
    – tchrist
    Mar 22, 2012 at 18:43
1

You probably need to use setlocale for LC_CTYPE before such conversions will be done correctly, but there is also the issue of what encoding your string is in. ucwords is only meant to work on single-byte-encoded text.

0

Have a look at Kohana UTF8 class - http://kohanaframework.org/3.2/guide/api/UTF8

1
  • Link-only recommendations are low-value and should be written as comments under the question. Dec 3, 2018 at 5:24
0

Use this:

function mb_ucwords ($string)
{
    return mb_convert_case ($string, MB_CASE_TITLE, 'UTF-8'); 
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.