非拉丁词的str

编程入门 行业动态 更新时间:2024-10-10 17:25:22
本文介绍了非拉丁词的str_word_count()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图计算以非拉丁语言(保加利亚语)写的变量中的字数。但似乎str_word_count()不计数非拉丁语。 PHP文件的编码为UTF-8

$ str =текстнакирилица echo'字数:'.str_word_count($ str); //这返回0

解决方案

使用regex:

$ str =текстнакирилица echo'字数:'.count(preg_split('/ \s + /',$ str));

这里我将字分隔符定义为空格字符。如果可能有其他的东西会被当作字分隔符,你需要将它添加到正则表达式中。

另外,请注意,由于没有utf字符在regex (不是字符串) - / u 修饰符不是必需的。但是如果你想要一些utf字符作为分隔符,你需要添加这个regex修饰符。

更新: p>

如果您只希望以字母对待西里尔字母,您可以使用:

$ str =текстна12453 кирилица; echo'字数:'.count(preg_split('/ [^А-Яа-яЁё] + / u',$ str)

im trying to count the number of words in variable written in non-latin language (Bulgarian). But it seems that str_word_count() is not counting non-latin words. The encoding of the php file is UTF-8

$str = "текст на кирилица"; echo 'Number of words: '.str_word_count($str); //this returns 0

解决方案

You may do it with regex:

$str = "текст на кирилица"; echo 'Number of words: '.count(preg_split('/\s+/', $str));

here I'm defining word delimiter as space characters. If there may be something else that will be treated as word delimiter, you'll need to add it into your regex.

Also, note, that since there's no utf characters in regex (not in string) - /u modifier isn't required. But if you'll want some utf characters to act as delimiter, you'll need to add this regex modifier.

Update:

If you want only cyrillic letters to be treated in words, you may use:

$str = "текст на 12453 кирилица"; echo 'Number of words: '.count(preg_split('/[^А-Яа-яЁё]+/u', $str));

更多推荐

非拉丁词的str

本文发布于:2023-11-30 07:53:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649248.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:str

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!