[英]Php function UTF-8 characters issue
Here is my function that makes the first character of the first word of a sentence uppercase: 这是我的功能,它使句子的第一个单词的第一个字符变为大写:
function sentenceCase($str)
{
$cap = true;
$ret = '';
for ($x = 0; $x < strlen($str); $x++) {
$letter = substr($str, $x, 1);
if ($letter == "." || $letter == "!" || $letter == "?") {
$cap = true;
} elseif ($letter != " " && $cap == true) {
$letter = strtoupper($letter);
$cap = false;
}
$ret .= $letter;
}
return $ret;
}
It converts "sample sentence" into "Sample sentence". 它将“样本句子”转换为“样本句子”。 The problem is, it doesn't capitalize UTF-8 characters. 问题是,它不能大写UTF-8字符。 See this example . 请参阅此示例 。
What am I doing wrong? 我究竟做错了什么?
The most straightforward way to make your code UTF-8 aware is to use mbstring
functions instead of the plain dumb ones in the three cases where the latter appear: 使代码知道UTF-8的最直接方法是在以下三种情况下使用mbstring
函数而不是普通的哑函数 :
function sentenceCase($str)
{
$cap = true;
$ret = '';
for ($x = 0; $x < mb_strlen($str); $x++) { // mb_strlen instead
$letter = mb_substr($str, $x, 1); // mb_substr instead
if ($letter == "." || $letter == "!" || $letter == "?") {
$cap = true;
} elseif ($letter != " " && $cap == true) {
$letter = mb_strtoupper($letter); // mb_strtoupper instead
$cap = false;
}
$ret .= $letter;
}
return $ret;
}
You can then configure mbstring
to work with UTF-8 strings and you are ready to go: 然后,您可以将mbstring
配置为与UTF-8字符串一起使用,就可以开始了:
mb_internal_encoding('UTF-8');
echo sentenceCase ("üias skdfnsknka");
Bonus solution 奖金解决方案
Specifically for UTF-8 you can also use a regular expression, which will result in less code: 专门针对UTF-8,您还可以使用正则表达式,这将减少代码量:
$str = "üias skdfnsknka";
echo preg_replace_callback(
'/((?:^|[!.?])\s*)(\p{Ll})/u',
function($match) { return $match[1].mb_strtoupper($match[2], 'UTF-8'); },
$str);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.