1

i need to split Arabic text am trying different methods but not solved it

$input = "ابجد";
$split = mb_str_split($input);
print_r($split);

am expected output like

(ا،ب،ج،د)

need solution

qaiserjutt
  • 25
  • 8
  • 1
    [How to debug Php code?](https://stackoverflow.com/questions/5710665/how-to-debug-php-code), When you use that you should get a warning like: "**Warning: Array to string conversion**", because the output of `str_split()` is an array, you should not use that value the way you do now.... – Luuk Jan 15 '23 at 15:11
  • Its because your file/form is in unicode character, that need to be in utf-8 .. Can you share your html code – SelVazi Jan 15 '23 at 15:13
  • sir its normal code am used in function not used in html am add in header utf-8 all other working fine but split not working – qaiserjutt Jan 15 '23 at 15:20

2 Answers2

2

You can try mb_str_split() which can process multibyte strings. This code:

$input = "ابجد";
$split = mb_str_split($input);
print_r($split);

results in:

Array
(
    [0] => ا
    [1] => ب
    [2] => ج
    [3] => د
)

When you manipulate (trim, split, splice, etc.) strings encoded in a multibyte encoding, you need to use special functions since two or more consecutive bytes may represent a single character in such encoding schemes. Otherwise, if you apply a non-multibyte-aware string function to the string, it probably fails to detect the beginning or ending of the multibyte character and ends up with a corrupted garbage string that most likely loses its original meaning.

You can reverse the output by using array_reverse(), like this:

$input = "ابجد";
$split = mb_str_split($input);
$reversed = array_reverse($split);
print_r($reversed);

The output now is:

Array
(
    [0] => د
    [1] => ج
    [2] => ب
    [3] => ا
)

See: https://3v4l.org/ZsJNb

KIKO Software
  • 15,283
  • 3
  • 18
  • 33
0

You can try implode for output=(ا،ب،ج،د) :

 $input = "ابجد";
 $split = mb_str_split($input,1,'UTF-8');
 print "(".implode('،',$split).")";
 //output: (ا،ب،ج،د)
ramin
  • 448
  • 4
  • 15