Home > Net >  mb_split returning strange characters for RTL
mb_split returning strange characters for RTL

Time:01-28

I'm trying to split Arabic words into array but it always return some strange characters. enter image description here Here's the code:

<?php
mb_internal_encoding("UTF-8");
function fatehah(){
    $surah = "
بِسْمِ ٱللَّهِ ٱلرَّحْمَـٰنِ ٱلرَّحِيمِ ١ٱلْحَمْدُ لِلَّهِ رَبِّ ٱلْعَـٰلَمِينَ ٢ٱلرَّحْمَـٰنِ ٱلرَّحِيمِ ٣مَـٰلِكِ يَوْمِ ٱلدِّينِ ٤إِيَّاكَ نَعْبُدُ وَإِيَّاكَ نَسْتَعِينُ ٥ٱهْدِنَا ٱلصِّرَٰطَ ٱلْمُسْتَقِيمَ ٦صِرَٰطَ ٱلَّذِينَ أَنْعَمْتَ عَلَيْهِمْ غَيْرِ ٱلْمَغْضُوبِ عَلَيْهِمْ وَلَا ٱلضَّآلِّينَ   ٧
    ";
    $words = mb_split("\s",$surah);
    //shuffle($words);
    for($i = 0; $i < count($words); $i  ){
        $word[] = $words[$i][0];
    }
    return $word;
}
$selwords = fatehah();
var_dump($selwords);
?>

CodePudding user response:

If you want to get only first letter of each word, use mb_substr() instead:

mb_internal_encoding("UTF-8");
function fatehah(){
    $surah = "
بِسْمِ ٱللَّهِ ٱلرَّحْمَـٰنِ ٱلرَّحِيمِ ١ٱلْحَمْدُ لِلَّهِ رَبِّ ٱلْعَـٰلَمِينَ ٢ٱلرَّحْمَـٰنِ ٱلرَّحِيمِ ٣مَـٰلِكِ يَوْمِ ٱلدِّينِ ٤إِيَّاكَ نَعْبُدُ وَإِيَّاكَ نَسْتَعِينُ ٥ٱهْدِنَا ٱلصِّرَٰطَ ٱلْمُسْتَقِيمَ ٦صِرَٰطَ ٱلَّذِينَ أَنْعَمْتَ عَلَيْهِمْ غَيْرِ ٱلْمَغْضُوبِ عَلَيْهِمْ وَلَا ٱلضَّآلِّينَ   ٧
    ";
    $words = mb_split("\s",$surah);
    //shuffle($words);
    
    return array_map(
        fn ($word) => mb_substr($word, 0, 1),
        $words,
    );
}
$selwords = fatehah();
var_dump($selwords);

Example


If you want to have each word in its own array:

mb_internal_encoding("UTF-8");
function fatehah(){
    $surah = "
بِسْمِ ٱللَّهِ ٱلرَّحْمَـٰنِ ٱلرَّحِيمِ ١ٱلْحَمْدُ لِلَّهِ رَبِّ ٱلْعَـٰلَمِينَ ٢ٱلرَّحْمَـٰنِ ٱلرَّحِيمِ ٣مَـٰلِكِ يَوْمِ ٱلدِّينِ ٤إِيَّاكَ نَعْبُدُ وَإِيَّاكَ نَسْتَعِينُ ٥ٱهْدِنَا ٱلصِّرَٰطَ ٱلْمُسْتَقِيمَ ٦صِرَٰطَ ٱلَّذِينَ أَنْعَمْتَ عَلَيْهِمْ غَيْرِ ٱلْمَغْضُوبِ عَلَيْهِمْ وَلَا ٱلضَّآلِّينَ   ٧
    ";
    $words = mb_split("\s",$surah);
    //shuffle($words);
    
    return array_map(
        fn ($word) => [$word],
        $words,
    );
}
$selwords = fatehah();
var_dump($selwords);

Example


If you want just array of word, simply do return mb_split("\s", $surah)

  •  Tags:  
  • php
  • Related