3

I have something like

$a = "बिक्रम"

I want to achieve something like in php

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

now i want to count the letter of hindi name in a varial and how many vowel in hindi name(string). how to break hindi string in array with php and count how many letter and vowel in string Index page

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

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


?>
Alok Gupta
  • 81
  • 7

1 Answers1

3

An Approach But Cant Print Exact output as 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 function which iterates on an array returned by mbStringToArray() and if next element is equal to a Varnamala(वर्णमाला) like ि, ् etc join it together with the previous.

here is an 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] => म )

Shub
  • 2,686
  • 17
  • 26
  • 1
    hello sir i am using your method but output come like :- Array ( [0] => & [1] => # [2] => 2 [3] => 3 [4] => 2 [5] => 5 [6] => ; [7] => & [8] => # [9] => 2 [10] => 3 [11] => 6 [12] => 7 [13] => ; [14] => & [15] => # [16] => 2 [17] => 3 [18] => 5 [19] => 2 [20] => ; [21] => & [22] => # [23] => 2 [24] => 3 [25] => 8 [26] => 1 [27] => ; [28] => & [29] => # [30] => 2 [31] => 3 [32] => 4 [33] => 0 [34] => ; [35] => & [36] => # [37] => 2 [38] => 3 [39] => 6 [40] => 7 [41] => ; ) what is reason for that and what is solution please reply – Alok Gupta Nov 03 '14 at 08:48
  • if you have any problem share code its impossible to predict what could be the problem without looking at the code. – Shub Nov 03 '14 at 09:02
  • sir your code work in your site but when i put your code in my notepad++ file it show like:- $a = "??????"; – Alok Gupta Nov 03 '14 at 09:13
  • its because of character encoding the simplest thing you can do is within notepad++ create a new file copy whole previous code +above code and then save it and replace it with previous one or with a new name. – Shub Nov 03 '14 at 09:16
  • or within the document click on encoding menu at top and select encode in UTF-8 and then paste the code . – Shub Nov 03 '14 at 09:20
  • subhanker i try but output come like:- Array ( [0] => ? [1] => ? [2] => ? [3] => ? [4] => ? [5] => ? ) Array ( [0] => ?????? ) from both function – Alok Gupta Nov 03 '14 at 10:18
  • There may either be an problem with your browser make shure character encoding is set to utf-8,well are you testing on command line ? – Shub Nov 03 '14 at 12:49
  • please check my code there are two page first is index.php and second is demo_get.php when we run that code it will show ecoded array. not like (Array ( [0] => बि [1] => क् [2] => र [3] => म )) – Alok Gupta Nov 03 '14 at 13:09
  • well in my browser seems its working perfectly [image](http://i.stack.imgur.com/g6M0z.jpg),And in your code you have not used `joinVarnamala()` so you are getting vernmalas saperately. – Shub Nov 03 '14 at 13:36
  • yes it is my browser problem. but some work is left in demo. can you solve this? I want counting in numbers like :- i put a name आलोक i want output like letter=3 and vowel=2 because in आलोक first letter (आ), second letter (लो) and third letter is (क). so out put become is 3 and for vowel , first vowel ( ा) and second vowel( ो) so out put vowel=2 – Alok Gupta Nov 04 '14 at 06:06
  • well I can give you an hint creat an array of the vovels in an array and use in_array() function check for number ov vovles in it – Shub Nov 04 '14 at 10:38