skip to Main Content

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

2

Answers


  1. 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

    Login or Signup to reply.
  2. You can try implode for output=(ا،ب،ج،د) :

     $input = "ابجد";
     $split = mb_str_split($input,1,'UTF-8');
     print "(".implode('،',$split).")";
     //output: (ا،ب،ج،د)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search