如何在 PHP 中将此字符串操作函数转换为 UTF-8 兼容?

发布于 2024-09-27 11:05:21 字数 1036 浏览 5 评论 0原文

我很难找到一个完全符合我需要的功能。不幸的是,这个函数不兼容 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幽蝶幻影 2024-10-04 11:05:21

你的问题是ucwords。在 php 页面上的快速搜索让我发现了这一点:

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

我测试过,它工作得很好,只需记住这一行:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

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" />
我们的影子 2024-10-04 11:05:21

好吧,您需要交换一些功能。首先,UTF-8 没有 str_replace 替代方案(您可能需要也可能不需要)。您应该将 ucwords 替换为 mb_convert_casestrlenmb_strlen...

但是有比循环多次更有效的方法:

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

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文