Php 在句子中数数字母

Php 在句子中数数字母,php,Php,如何使用PHP计算下面这样一个句子中的字母数 你好,你好吗 strlen给出包括空格在内的总数。您可以先删除所有非字母: $result = preg_replace('/\P{L}+/u', '', $subject); 然后执行mb\u strlen($result) \p{L}是“任意Unicode字母”的正则表达式 \p{L}是它的倒数-除了字母以外的任何东西。您可以先删除所有非字母: $result = preg_replace('/\P{L}+/u', '', $subject);

如何使用PHP计算下面这样一个句子中的字母数

你好,你好吗


strlen
给出包括空格在内的总数。

您可以先删除所有非字母:

$result = preg_replace('/\P{L}+/u', '', $subject);
然后执行
mb\u strlen($result)

\p{L}
是“任意Unicode字母”的正则表达式


\p{L}
是它的倒数-除了字母以外的任何东西。

您可以先删除所有非字母:

$result = preg_replace('/\P{L}+/u', '', $subject);
然后执行
mb\u strlen($result)

\p{L}
是“任意Unicode字母”的正则表达式


\p{L}
是它的倒数-除了字母以外的任何东西。

重写不计算空格的
strlen

重写不计算空格的
strlen

你可以用以下方法去掉空格:


然后就可以很容易地计算出剩余的字符。

您可以使用以下方法去掉空格:

$string = 'hello how are you';
$spaceless = str_replace(' ','',$string);
$totalcharswithspaces = strlen($string);
$totalchars = strlen($spaceless);

然后就可以轻松地计算剩余的字符了。

$letter\u count=strlen($string)-substr\u count($string,)

$string = 'hello how are you';
$spaceless = str_replace(' ','',$string);
$totalcharswithspaces = strlen($string);
$totalchars = strlen($spaceless);

这是总长度-空格数。

$letter\u count=strlen($string)-substr\u count($string,)


这是总长度-空格数。

如果还需要频率信息


$strip_chars = array(" "); //this is expandable now
$array_of_used_characters = count_chars(str_replace($strip_chars, '', $sentence), 1);
var_dump($array_of_used_characters);

如果你也需要频率信息


$strip_chars = array(" "); //this is expandable now
$array_of_used_characters = count_chars(str_replace($strip_chars, '', $sentence), 1);
var_dump($array_of_used_characters);

您只需要字母还是任何非空格字符?您只需要字母还是任何非空格字符?哎呀。:)很抱歉我太累了,不能做这个很抱歉我太累了。为什么要麻烦使用unicode安全正则表达式,通过使用
strlen()
,可能会得到错误的字母数?@salathe:噢,所以
strlen()
计算字节数,而不是字符数(我不知道PHP)?是否有一个可以正确处理Unicode字符串的函数?
mb_strlen()
。所有支持多字节的字符串函数都在
mb.
前缀下。为什么要麻烦使用unicode安全正则表达式,通过使用
strlen()
,可能会获得错误的字母数?@salate:Oh,那么
strlen()
计算字节数,而不是字符数(我不知道PHP)?是否有一个可以正确处理Unicode字符串的函数?
mb_strlen()
。所有支持多字节的字符串函数都在
mb
前缀下。