如何在 PHP 中将此字符串操作函数转换为 UTF-8 兼容?
我很难找到一个完全符合我需要的功能。不幸的是,这个函数不兼容 UTF-8。这个函数就像一个基本的 ucwords ,但它也对字符进行大写,后跟找到的给定字符之一(在我的例子中,我需要对在 之后找到的字符应用大写) -
)。
这是函数:
<?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));
?>
一旦我向字符串添加重音符号,例如:
echo my_ucwords(strtolower( "saint-étienne" )) //return: Saint- instead of Saint-Étienne
有什么想法吗?我知道我可以使用 mb_strlen
代替 strlen
。但其他人呢?
编辑: 只是提醒一下,我不仅需要一个以 UTF-8
工作的简单 ucwords
。我需要它对 -
之后找到的任何字符应用大写。
我也在尝试自己解决这个问题。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的问题是ucwords。在 php 页面上的快速搜索让我发现了这一点:
我测试过,它工作得很好,只需记住这一行:
Your problem is ucwords. A quick search on the php page made me discover this:
I tested and it works perfectly just remember this line:
好吧,您需要交换一些功能。首先,UTF-8 没有
str_replace
替代方案(您可能需要也可能不需要)。您应该将 ucwords 替换为mb_convert_case
和strlen
与mb_strlen
...但是有比循环多次更有效的方法:
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 replaceucwords
withmb_convert_case
andstrlen
withmb_strlen
...But there are more efficient ways to do it than looping several times: