skip to Main Content

am trying to remove duplicates values from Arabic string input but

`$input = "اللہ";
$step1 = preg_split("/(?<!^)(?!$)/u", $input);
$step2 = implode(' ',step1);

$step3 = array_unique(step2 );

echo "step3";`

i need output like this

ا ل ھ

2

Answers


  1. Can you try this?

    $input = "اللہ";
    $step1 = preg_split("/(?<!^)(?!$)/u", $input);
    $step2 = implode(' ',$step1);
    $step3 = str_split($step2);
    $step4 = array_unique($step3);
    $result = implode('', $step4);
    echo "$result";
    
    Login or Signup to reply.
  2. Instead of running a regular expression, you could use the multibyte function mb_str_split() from the mb_string library. It is made to handle special charsets. Your PHP config might already enable it to replace usual PHP functions, but in this case you should check your INI file, or you can add some ini_set() calls at the beginning of your code. But the easiest is just to call the mb_* functions.

    Secondly, as @CBroe pointed out, you made a few mistakes in your code by passing a string to a function that wants an array as parameter.

    This is what you can do:

    <?php
    
    header('Content-Type: text/plain; charset=UTF-8');
    
    $input = "اللہ";
    $step1 = mb_str_split($input, 1, 'UTF-8');
    print 'mb_str_split() returns ' . var_export($step1, true) . PHP_EOL;
    
    $step2 = array_unique($step1);
    print 'array_unique() returns ' . var_export($step2, true) . PHP_EOL;
    
    print 'Desired output string is "' . implode(' ', $step2) . '"' . PHP_EOL;
    

    Output:

    mb_str_split() returns array (
      0 => 'ا',
      1 => 'ل',
      2 => 'ل',
      3 => 'ہ',
    )
    array_unique() returns array (
      0 => 'ا',
      1 => 'ل',
      3 => 'ہ',
    )
    Desired output string is "ا ل ہ"
    

    You can run it here: https://onlinephp.io/c/fe990

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search