-1

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

ا ل ھ

Qaiser420
  • 11
  • 6
  • 2
    Absolutely wrong conclusion: _"array unique function not working on Arabic alphabets"_ - Correct view at the specific problem here: array_unique does not work on _strings_ - which is what implode gives you as a result. – CBroe Jan 30 '23 at 08:35
  • Related: [Multibyte-safe count distinct characters in a string](https://stackoverflow.com/q/7730059/2943403) and [Convert a String into an Array of Characters - multi-byte](https://stackoverflow.com/q/55782088/2943403) – mickmackusa May 28 '23 at 00:47

2 Answers2

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";
Tech Geek
  • 11
  • 2
  • sir not working its only working for 5 to 6 string input not working on large values @Tech Geek – Qaiser420 Jan 30 '23 at 11:25
  • here is test see the error [https://paiza.io/projects/Lw4RuCdx0LPCIXGpHRymzg?language=php] please guide me why not working with more then 10 strings @Tech Geek – Qaiser420 Jan 30 '23 at 11:51
0

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

Patrick Janser
  • 3,318
  • 1
  • 16
  • 18
  • not working sir – Qaiser420 Jan 30 '23 at 11:20
  • Ok, could you explain and give us some details? I don't know arabic letters so it's also complicated for us to see what would be correct or incorrect. Can you give us a playground URL to try and solve your problem? What did you try yourself? – Patrick Janser Jan 30 '23 at 11:26
  • here is link check it sir [https://paiza.io/projects/ltQdnz1gWJF9yBIiVV5_dg] you make your solution play with echo may be you understand not print make it in echo when am start in php file and run it no thing show on page @Patrick Janser check the link https://paiza.io/projects/ltQdnz1gWJF9yBIiVV5_dg – Qaiser420 Jan 30 '23 at 11:42
  • @Qaiser420 1) First link is broken. 2) Second link doesn't work because you left or added **code here!** in my PHP code... Remove it and run it! – Patrick Janser Jan 30 '23 at 12:03
  • ok sir am find the error why your code not working its php version problem your code working only on php 7.4 to up version i have php 5 .4 version please check it please give me solution in lower php version @Patrick Janser – Qaiser420 Jan 30 '23 at 12:38
  • Sir @Qaiser420, **[PHP 5.4](https://www.php.net/releases/#5.4.45) is more than 7 years old!** You shouldn't run such an old version as you'll have security problems. Update your PHP installation if you can. If not, **have you enabled the mbstring extension**? – Patrick Janser Jan 30 '23 at 13:09
  • thanks sir am update version working perfect [thanks] – Qaiser420 Jan 30 '23 at 13:30