简体   繁体   English

如何计算所有字符,包括空格和php中的正则表达式?

[英]How to count all characters including spaces and regex in php?

how can I count all characters? 如何计算所有字符? I am selecting a field from mysql (which is a plain text) and I would like to count all letters in that text that is extracted (including spaces and all special characters). 我正在从mysql(这是纯文本)中选择一个字段,我想计算提取的文本中的所有字母(包括空格和所有特殊字符)。

I am comparing the value with a number, for example: 我正在将值与数字进行比较,例如:

If text has more than 1000 characters then $var++ so I know how many fields have more than 1000 characters. 如果文本具有超过1000个字符,则$var++因此我知道有多少个字段具有超过1000个字符。

strlen does not seems to be counting right. strlen似乎不正确。

如果您具有非ASCII字符,请使用strlenmb_strlen

You need mb_strlen() for Unicode characters: http://php.net/manual/en/function.mb-strlen.php 您需要mb_strlen()来输入Unicode字符: http : mb_strlen()

On the other hand if you're just counting how many entries in the database have more than 1000 characters, you can save some computing power with the MySQL function CHAR_LENGTH() to count the total in the database: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_char-length 另一方面,如果仅计算数据库中有多少个条目超过1000个字符,则可以使用MySQL函数CHAR_LENGTH()节省一些计算能力,以计算数据库中的总数: http://dev.mysql .com / doc / refman / 5.0 / zh-CN / string-functions.html#function_char-length

This probably happens because of encoding issues. 这可能是由于编码问题而发生的。 This has nothing to do with regex s. 这与regex无关。

You can use mb_strlen to get the correct length. 您可以使用mb_strlen来获取正确的长度。

You could let mysql do the counting as it knows the charset, has a counting function and can even give you the number of fields in a table that have more then 1000 characters directly: 您可以让mysql进行计数,因为它知道字符集,具有计数功能,甚至可以直接为您提供表中具有超过1000个字符的字段数:

SELECT count(*) as var FROM table WHERE CHAR_LENGTH(field) > 1000;

This will return your var as mysql result. 这将返回您的var作为mysql结果。 This will spare you to return the whole table data just to obtain this number. 这将使您免于返回整个表数据,只是为了获得该编号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM