Home > Software engineering >  Alphabets to number converter but non english letter problem
Alphabets to number converter but non english letter problem

Time:06-17

Hello all i need help i create small code alphabets to numbers code working perfect on english to numbers but when am trying to put arabic urdu alphabet to numbers function not working please help my problem in arabic need help

THIS CODE Working Perfect on english alphabet

$input = "JHON";
 
$remap = [
    "a" => '1',
    "A" => '1',
    "b" => '2',
    "B" => '2',
    "c" => '3',
    "C" => '3',
    "J" => '100',
    "H" => '10',
    "O" => '90',
    "N" => '200',
];
 
$array = [];
for ($i = 0; $i < strlen($input); $i  ) {
    $c = $input[$i];
    $array[] = $remap[$c];
}
$star = "(" . implode(',', $array) . ")";
echo $star;

now problem is here when am put arabic alphabet in array not working

$input = "ب";
 
$remap = [
    "ا" => '200',
    "ب" => '300',
    "ج" => '50',
    "د" => '100',
];
 
$array = [];
for ($i = 0; $i < strlen($input); $i  ) {
    $c = $input[$i];
    $array[] = $remap[$c];
}
$star = "(" . implode(',', $array) . ")";
echo $star;

the answer is 300 but output is grabage etc please tell me how i can manage it

CodePudding user response:

String indexing with [] is not multibyte-safe. You need to use the mb_XXX functions when processing languages like Arabic.

<?php
$input = "ب";

$remap = [
    "ا" => '200',
    "ب" => '300',
    "ج" => '50',
    "د" => '100',
];

$array = [];
for ($i = 0; $i < mb_strlen($input); $i  ) {
    $c = mb_substr($input, $i, 1);
    $array[] = $remap[$c];
}
$star = "(" . implode(',', $array) . ")";
echo $star;
  •  Tags:  
  • php
  • Related