Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java等价于PHP str\u word\u count?_Java_Php_Word Count - Fatal编程技术网

Java等价于PHP str\u word\u count?

Java等价于PHP str\u word\u count?,java,php,word-count,Java,Php,Word Count,注意:我不在这里寻找Java中的字数计算算法。我在问PHP源代码中的str\u word\u count 我希望将PHP函数作为Java方法重新创建(逐字逐行,如果可能的话): public class PhpWordCounter { public int strWordCount(String str) { // The exact algorithm used in PHP's str_word_count here... // The str_wo

注意:我不在这里寻找Java中的字数计算算法。我在问PHP源代码中的
str\u word\u count

我希望将PHP函数作为Java方法重新创建(逐字逐行,如果可能的话):

public class PhpWordCounter {
    public int strWordCount(String str) {
        // The exact algorithm used in PHP's str_word_count here...
        // The str_word_count method takes an optional parameter
        // "format", which, if 0, returns the number of words.
        // This is the portion of the routine that I will actually
        // be mimicking.
    }
}

我刚刚下载了PHP5.3.23的源代码(tarball)并进行了解压缩。我搜索了
str\u word\u count
,但什么也没找到,所以我甚至不确定要查找什么,更不用说搜索哪个文件了。有人有什么想法吗?提前谢谢

我不知道是否有现成的功能。。。 但是: 将字符串按空格拆分为数组 获取数组的长度

字符串VAL[]=myString.split(“\s+”);
int wordCount=vals.length

嗯,你应该看起来更好:)它应该在最新的stable branch中提供


如果您的目标是精确复制,那么返回类型肯定不是int。Good catch@Perception(+1)-我应该提到,
格式在默认情况下被视为0(因此将字数作为整数类型返回)。因此,我不会担心
str\u word\u count
中处理任何其他
格式
类型的任何部分。PHP源代码是可用的,用C编写。如果你想复制精确的算法,那就是开始的地方。对,但是就像我说的,我查找了
str\u word\u count
,但没有找到它…@DirtyMikeAndTheBoys-它看起来是在php-5.x.xx/ext/standard/string.c中实现的。或者至少是其中的一部分。你考虑过使用吗?它有一个
countMatches
方法,已经完成了这个逻辑。谢谢,但是请看我的更新-我不是要Java单词计数算法。我问在PHP源代码中哪里可以找到str_word_count。谢谢@tlnse这正是我需要的
find . -type f -exec grep -l 'str_word_count' {} \;

./ext/standard/php_string.h
./ext/standard/tests/strings/str_word_count1.phpt
./ext/standard/tests/strings/str_word_count.phpt
./ext/standard/basic_functions.c
./ext/standard/string.c