开发者

How to convert this string manipulation function UTF-8 Compatible in PHP?

开发者 https://www.devze.com 2023-01-19 12:48 出处:网络
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 does the uppercase on

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 does 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, e.g.:

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

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

Edit: Just a reminder 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 it out by myself, too.


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" />


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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消