Cyb*_*rix 3 php string utf-8 diacritics
我找不到一个完全符合我要求的功能.不幸的是,这个功能不兼容UTF-8.这个函数就像一个基本函数,ucwords但它也会在一个字符后面跟着一个给定的字符(在我的情况下我需要在a之后的字符上应用大写-).
这是功能:
<?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));
?>
Run Code Online (Sandbox Code Playgroud)
只要我在我的字符串中添加重音符号,例如:
echo my_ucwords(strtolower( "saint-étienne" )) //return: Saint- instead of Saint-Étienne
Run Code Online (Sandbox Code Playgroud)
任何的想法?我知道而不是strlen我可以使用的mb_strlen.但其他人呢?
编辑:
提醒我,我不仅需要简单的ucwords工作UTF-8.我需要它来在一个后面找到的任何字符上应用大写-.
我还在试图自己解决这个问题.
你的问题是ucwords.在php页面上快速搜索让我发现了这个:
function mb_ucwords($str) {
return mb_convert_case($str, MB_CASE_TITLE, "UTF-8");
}
Run Code Online (Sandbox Code Playgroud)
我测试过,它完美地运行,只需记住这一行:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Run Code Online (Sandbox Code Playgroud)