How to split a hindi string in an array with php and count how many letters and vowels are in the string

I have something like

$a = "बिक्रम"

      

I want to achieve something like php

a[0] = बि 
a[1] = क्र 
a[3] = म

      

Now I want to count the letter of a Hindi name in a varial and how many vowels there are in a Hindi name (string). how to split hindi string in array with php and count how many letters and vowels are in the string Home

<html>

<head>
  <script src="http://www.hinkhoj.com/common/js/keyboard.js"></script>
  <link rel="stylesheet" type="text/css" href="http://www.hinkhoj.com/common/css/keyboard.css" />
</head>

<body align="center" style="margin-top:15%">
  Name :&nbsp;&nbsp;&nbsp;&nbsp;
  <script language="javascript">
    CreateCustomHindiTextBox("nameid", "", 40, true);

    function ff() {
      var a = document.getElementById("nameid").value;
      var xmlhttp;
      if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
      } else { // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
          document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
        }
      }

      xmlhttp.open("GET", "demo_get.php?val=" + a, true);
      xmlhttp.send();

    }
  </script>

  <input type="submit" name="submit" onClick="ff()">

  <div id="myDiv"></div>
</body>

</html>
      

Run codeHide result


ajax page

<?php

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;
} 
echo $name = $_GET['val'];

print_r(mbStringToArray($name));


?>
      

Run codeHide result


+3


source to share


1 answer


Approach, but Cant Print Exact conclusion mentioned

<?php
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));

      

Output:

Array ([0] => ब [1] => ि [2] => क [3] => ् [4] => र [5] => म)

What you can do is create a function that iterates over the array returned mbStringToArray()

, and if the next element is Varnamala (वर्णमाला), for example ि, ्

, etc., join it along with the previous one.



here is the approach.

function joinVarnamala($array){
    $singsArray=array("ि","्");
    $out=array($array[0]);

    for ($i=1; $i <count($array) ; $i++) { 
        if (in_array($array[$i], $singsArray)) {
            $out[count($out)-1].=$array[$i];
        }else{
            $out[]=$array[$i];
        }
    }
    return $out;
}
print_r(joinVarnamala($ar));

      

output:

Array ([0] => बि [1] => क् [2] => र [3] => म)

+2


source







All Articles