问题
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 :
<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));
?>
回答1:
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 an 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] => म )
来源:https://stackoverflow.com/questions/26689852/how-to-break-hindi-string-in-array-with-php-and-count-how-many-letter-and-vowel