Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

mb_split returning strange charadters for RTL

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);
?>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading