2013-03-19 67 views
0

注意:我不在這裏尋找Java中的字數統計算法。我在問哪裏可以找到PHP源代碼中的str_word_countPHP的等價物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的源代碼(tar包)和提取。我grepped爲str_word_count,並沒有找到任何東西,所以我甚至不知道要尋找什麼,更不用說尋找什麼文件。任何人有任何想法?提前致謝!

+1

很好地可用,返回類型鐵定*** ***不能是int,如果你的目標是*確切*複製。 – Perception 2013-03-19 10:40:20

+0

良好的捕獲@Perception(+1) - 我應該提到'格式'被默認處理爲0(因此將字數作爲整數類型返回)。所以我不會擔心處理任何其他'format'類型的'str_word_count'的任何部分。 – IAmYourFaja 2013-03-19 10:42:25

+0

PHP源代碼可用,用C編寫。如果你想複製確切的算法,那就從哪裏開始。 – SDC 2013-03-19 10:44:38

回答

2

那麼你應該尋找更好的:)應該在最新的穩定分支http://www.php.net/manual/en/function.str-word-count.php

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 
+0

謝謝@tlnse那正是我需要的 – IAmYourFaja 2013-03-19 10:51:14

+0

不客氣! – tlenss 2013-03-19 10:52:35

0

我不知道是否有一個現成的函數... 但: 用空格的字符串分割成一個數組 獲取數組的長度

字符串瓦爾斯[] = myString.split ( 「\ S +」); int wordCount = vals.length;

+0

謝謝,但請看我的更新 - 我不是要求Java字數統計算法。我在問哪裏可以找到PHP源代碼中的str_word_count。 – IAmYourFaja 2013-03-19 10:46:55