views:

44

answers:

4

Good day,

I had trouble finding a function that does exactly what I am looking for. Unfortunatly, this function isn't UTF-8 Compatible. This functions is like a basic ucwords but it also do the uppercase on a character followed by one of the given characters found (in my case I need to apply an uppercase on the character found after a -

Here is the function:

<?php
function my_ucwords($string)
  {
    $noletters='"([/-'; //add more if u need to
    for($i=0; $i<strlen($noletters); $i++)
      $string = str_replace($noletters[$i], $noletters[$i].' ', $string);
    $string=ucwords($string);
    for($i=0; $i<strlen($noletters); $i++)
      $string = str_replace($noletters[$i].' ', $noletters[$i], $string);
    return $string;
  }

$title = 'ELVIS "THE KING" PRESLEY - (LET ME BE YOUR) TEDDY BEAR';
echo my_ucwords(strtolower($title));
?>

As soon as I add accents to my string eg:

echo my_ucwords(strtolower( "saint-étienne" )) //return: Saint- instead of Saint-Étienne

Any idea guys? I know instead of the strlen I could use mb_strlen. But what about the others?

Edit: Just a reminder guys that I do not only need a simple ucwords working in UTF-8. I need it to apply the uppercase on any character found after a -

I'm still trying to figure out on my side too.

A: 

I think it's what you need : http://www.php.net/manual/en/function.mb-convert-case.php

MatTheCat
+1  A: 

Your problem is ucwords. A quick search on the php page made me discover this:

function mb_ucwords($str) {
    return mb_convert_case($str, MB_CASE_TITLE, "UTF-8");
}

I tested and it works perfectly just remember this line:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
0plus1
When I try your function echo mb_ucwords("saint-étienne") I get Saint-Tienne... I want a function that will return Saint-Étienne
Cybrix
Oops!... its working if I do echo mb_ucwords( utf8_encode("saint-étienne") ); Thank you sir!
Cybrix
@Cybrix if you need to encode the string there's something wrong with the charset of the page, always rememeber to add the meta tag on the webpage!
0plus1
A: 

Well, you'd need to swap a few functions. First, there is no str_replace alternative for UTF-8 (you may or may not need it). You should replace ucwords with mb_convert_case and strlen with mb_strlen...

But there are more efficient ways to do it than looping several times:

function my_ucwords($string) {
    $chrs = '"([/-';
    $searchRegex = '/('.preg_quote($chrs, '/').')/u';
    $replaceRegex = '/('.preg_quote($chrs, '/').')\s/u';
    $tmpString = preg_replace($searchRegex, '\1 ', $string);
    $tmpString = mb_convert_case($tmpString, MB_CASE_TITLE);
    return preg_replace($replaceRegex, '\1', $tmpString);
}
ircmaxell
Doh! I get a Warning: preg_replace() [function.preg-replace]: Unknown modifier '-'
Cybrix
@Cybrix: fixed it in the code. I forgot that `preg_quote` doesn't have a default delimiter (I thought it defaulted to `/`, but I was wrong)...
ircmaxell
Yep working but now I get 'Saint-éTienne' when using: 'echo my_ucwords( utf8_encode("saint-étienne") );'
Cybrix
I now get `Saint-éTienne` when I run `echo my_ucwords( utf8_encode("saint-étienne") );`
Cybrix
Why are you using `utf8_encode`? It doesn't do what you think it does... Just run the raw through (or at worst case, use `mb_convert_encoding`, but `utf8_encode` doesn't work like that)...
ircmaxell