如何用php计算数组中的印地语字符串以及字符串中有多少字母和元音


how to count hindi string in array with php and count how many letter and vowel in string

我有类似的东西

$a = "आलोक"

我想在php 中实现类似的功能

a[0] = आ  
a[1] = लो  
a[3] = क 

我想要像这样的数字计数:-我写了个名字आलोक我想要像字母=3和元音=2这样的输出因为在आलोक第一个字母(आ),第二个字母(लो)第三个字母是(क).所以输出变为字母=3对于元音,第一个元音(ा)和第二元音(ो)所以输出元音=2

名称可以是动态的,而不是静态的

我正在研究你发布的另一个问题,接受的答案建议在以下几行使用一个函数将字符串分解为字符:

 function mbStringToArray ($string) {
  $strlen = mb_strlen($string);
   while ($strlen) {
    $array[] = mb_substr($string,0,1,"UTF-8");
    $string = mb_substr($string,1,$strlen,"UTF-8");
    $strlen = mb_strlen($string);
  }
  return $array;
 } 
  $a = "आलोक"; 
  print_r(mbStringToArray($a));

如果您运行此代码,它将为您提供以下输出:

  Array
  (
   [0] => आ
   [1] => ल
   [2] => ो
   [3] => क
  )

我将在这个函数的基础上进行扩展,这样你就可以很容易地计算元音和辅音了。

值得庆幸的是,我找到了这个关于Devnagri脚本中所有字符的UTF-8编码的方便指南。另一个简单的工具来确认和获取这些字符的十进制和八进制表示是http://unicodelookup.com/.

从表中,我查找了0x093F,并很容易地将其与ि.

现在,一旦你有了这个,只需要从HEX代码中获得解码的unicode字符。您可以通过以下方式轻松实现:

echo json_decode('"'u093F"'); //Ouputs  ि

我将这些步骤组合在一个名为countVowels:的函数中

 function countVowels ($req){
   //I have hard coded the hex values of some characters that are vowels in Hindi
   //This does NOT include all the vowels
   //You might want to add more as per your needs from the table that I have provided before
   $hindi = array("'u0906","'u0908","'u093E","'u093F","'u0945","'u0946","'u0947","'u0948","'u0949","'u094A","'u094B","'u094C","'u094D");
   $vowels= array();
   $vowelcount = 0;
   for($i = 0; $i<count($hindi); $i++){
     //Push the decoded unicode character into the $vowels array
     array_push($vowels,json_decode('"'.$hindi[$i].'"')); 
   }
   for($j=0;$j<count($req);$j++){
      if(in_array($req[$j], $vowels))
        $vowelcount++;
   }
   return $vowelcount;
 }

该函数的输入是$req,其可以是先前定义的函数mbStringToArray的输出阵列。一旦你有了元音的计数,你就可以很容易地得到其他辅音的计数。流程可能看起来像:

  $a = "आलोक"; 
  $arr = mbStringToArray($a)
  $vows = countVowels($arr); //Number of vowels 
  $cons = count($arr) - $vows; //Number of consonants

所以在这种情况下,返回的辅音是2,元音也是2。那是因为我已经硬编码了आ作为元音,因此它在CCD_ 5函数中被考虑。看看工作演示。

你可以修改我在那里使用的数组,并根据你的要求处理这些差异。我希望这能让你朝着正确的方向开始。