PHP 中 array_count_values 的错误结果

Wrong result for array_count_values in PHP

我有一个这样的数组 -

Array ( [0] => একটু [1] => আরো [2] => নয় [3] => কাছে [4] => থাকো [5] => না [6] => কাছে [7] => ডাকো [8] => না [9] => আরো [10] => কাছে [11] => আজ [12] => দুজনের [13] => প্রথম [14] => মিলনের [15] => চাঁদ [16] => তারা [17] => ঐ [18] => সাক্ষী [19] => আছে [20] => তুমি [21] => আমার [22] => আমি [23] => তোমার [24] => [25] => মন [26] => যে [27] => ভরেছে [28] => জোছনা [29] => ঝরেছে [30] => পৃথিবী [31] => দ্যাখো [32] => ঐ [33] => ঘুমিয়ে [34] => পড়েছে [35] => মন [36] => যে [37] => ভরেছে [38] => জোছনা [39] => ঝরেছে [40] => পৃথিবী [41] => দ্যাখো [42] => ঐ [43] => ঘুমিয়ে [44] => পড়েছে [45] => আমি [46] => তোমার [47] => তুমি [48] => আমার [49] => একটু [50] => আরো [51] => নয় [52] => কাছে [53] => থাকো [54] => না [55] => কাছে [56] => ডাকো [57] => না [58] => আরো [59] => কাছে [60] => আমি [61] => তোমার [62] => তুমি [63] => আমার [64] => [65] => ফুলেরই [66] => হাসিতে [67] => হাওয়ার [68] => বাঁশিতে [69] => এ [70] => রাত [71] => শেখালো [72] => ভালো [73] => যে [74] => বাসিতে [75] => আমি [76] => তোমার [77] => তুমি [78] => আমার [79] => একটু [80] => আরো [81] => নয় [82] => কাছে [83] => থাকো [84] => না [85] => কাছে [86] => ডাকো [87] => না [88] => আরো [89] => কাছে [90] => তুমি [91] => আমার [92] => আমি [93] => তোমার [94] => )

当我使用以下代码计算相同的值时,它会给出错误的结果,如下所示 -

$wordCountArr = array_count_values($matchWords);

Array ( [একটু] => 1 [আরো] => 6 [নয়] => 3 [কাছে] => 6 [থাকো] => 3 [না] => 6 [ কাছে] => 3 [ডাকো] => 3 [ আজ] => 1 [দুজনের] => 1 [প্রথম] => 1 [মিলনের] => 1 [ চাঁদ] => 1 [তারা] => 1 [ঐ] => 3 [সাক্ষী] => 1 [আছে] => 1 [ তুমি] => 2 [আমার] => 5 [আমি] => 2 [তোমার] => 5 [] => 3 [ মন] => 1 [যে] => 3 [ভরেছে] => 2 [জোছনা] => 2 [ঝরেছে] => 2 [ পৃথিবী] => 2 [দ্যাখো] => 2 [ঘুমিয়ে] => 2 [পড়েছে] => 2 [ মন] => 1 [ আমি] => 3 [তুমি] => 3 [ একটু] => 2 [ ফুলেরই] => 1 [হাসিতে] => 1 [হাওয়ার] => 1 [বাঁশিতে] => 1 [ এ] => 1 [রাত] => 1 [শেখালো] => 1 [ভালো] => 1 [বাসিতে] => 1 )

但是为什么 [কাছে] => 6 和 [কাছে] => 3 ???它应该是 [কাছে] => 9 现在我应该 trim 前后的空格还是用空格匹配单词?我用了 array_map('trim',$matchWords) 但没有运气! 我怎样才能解决这个问题?请帮忙!

是的,由于space,匹配是分开的。 trim() 删除每个单词的 spaces 不适用于您的情况,因为它不会删除 unicode white spaces.

因此您的解决方案使用:preg_replace.

参考:

Trim unicode whitespace

why is php trim is not really remove all whitespace and line breaks?