【发布时间】:2013-03-07 23:42:05
【问题描述】:
注意:我在这里不是在寻找 Java 中的字数统计算法。我在问在PHP源代码中哪里可以找到str_word_count。
我希望将str_word_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.
}
}
我刚刚下载了 PHP 5.3.23 的源代码(tarball)并解压。我寻找str_word_count 并没有找到任何东西,所以我什至不确定要查找什么,更不用说要搜索什么文件了。有人有什么想法吗?提前致谢!
【问题讨论】:
-
好吧,如果您的目标是精确复制,那么返回类型肯定不是是一个int。跨度>
-
Good catch @Perception (+1) - 我应该提到
format默认情况下被视为 0(因此将单词数作为整数类型返回)。所以我不会担心str_word_count中处理任何其他format类型的任何部分。 -
PHP 源代码可用,用 C 编写。如果您想复制确切的算法,那就从这里开始。
-
对,但就像我说的,我找了
str_word_count,但没有找到... -
@DirtyMikeAndTheBoys - 看起来实现在 php-5.x.xx/ext/standard/string.c 中。或者至少是其中的一部分。您是否考虑过使用Apache Commons Lang 3 StringUtils?它有一个
countMatches方法已经完成了这个逻辑。
标签: java php word-count