PHP

时间:2016-05-29 18:01:29

标签: php arrays array-map

我有一个这样的数组 -

  

数组([0] =>একটু[1] =>আরো[2] =>নয়[3] =>কাছে[4] =>থাকো[5] =>না[6] =>কাছে[7] =>ডাকো[8] =>না[9] =>আরো[10] =>কাছে[11] =>আজ[12] =>দুজনের[13] = >প্রথম[14] =>মিলনের[15] =>চাঁদ[16] =>তারা[17] =>ঐ[18] =>সাক্ষী[19] =>আছে[20] =&gt ;তুমি[21] =>আমার[22] =>আমি[23] =>তোমার[24] => [25] =>মন[26] =>যে[27] =>ভরেছে [28] =>জোছনা[29] =>ঝরেছে[30] =>পৃথিবী[31] =>দ্যাখো[32] =>ঐ[33] =>ঘুমিয়ে[34] =>পড়েছে[ 35] =>মন[36] =>যে[37] =>ভরেছে[38] =>জোছনা[39] =>ঝরেছে[40] =>পৃথিবী[41] =>দ্যাখো[42 ] =& gt;ঐ[43] =>ঘুমিয়ে[44] =>পড়েছে[45] =>আমি[46] =>তোমার[47] =>তুমি[48] =>আমার[49] =&gt ;একটু[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);
  

数组([একটু] => 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 现在我应该修剪前后空格还是用空格匹配单词?我使用array_map('trim',$matchWords)但没有运气! 我怎样才能解决这个问题?请帮忙!

1 个答案:

答案 0 :(得分:1)

是的,由于空间的原因,匹配是分开的。 Before Reverse Order : [A, B] After Reverse : [B, A] 删除每个单词的空格不适用于您的情况,因为它不会删除unicode空格。

所以你的解决方案使用:preg_replace

参见:

Trim unicode whitespace

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