Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Alphabets to number converter but non english letter problem

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$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

>Solution :

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;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading