Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove Arabic Diacritic

I want php to convert this...

Text : الْحَمْدُ لِلَّهِ رَبِّ الْعَالَمِينَ
converted to : الحمد لله رب العالمين 

I am not sure where to start and how to do it. Absolutely no idea. I have done some research, found this link http://www.suhailkaleem.com/2009/08/26/remove-diacritics-from-arabic-text-quran/ but it is not using php. I would like to use php and covert the above text to converted text. I want to remove any diacritic from user input arabic text

like image 650
Syed Sajid Avatar asked Aug 29 '14 06:08

Syed Sajid


People also ask

Does Arabic use diacritics?

Fully vocalised Arabic texts (i.e. Arabic texts with ḥarakāt/diacritics) are sought after by learners of Arabic. Some online bilingual dictionaries also provide ḥarakāt as a phonetic guide similarly to English dictionaries providing transcription.

How many Arab diacritics are there?

The fourteen diacritical marks listed in the table above are called عَلَامَات الشَّكْل 'alaamaat ash-shakl, which literally means the 'tools of controlling the word', namely controlling the pronunciation, the meaning, and the grammar.


2 Answers

The vowel diacritics in Arabic are combining characters, meaning that a simple search for these should suffice. There's no need to have a replace rule for every possible consonant with every possible vowel, which is a little tedious.

Here's a working example that outputs what you need:

header('Content-Type: text/html; charset=utf-8', true);
$string = 'الْحَمْدُ لِلَّهِ رَبِّ الْعَالَمِينَ';

$remove = array('ِ', 'ُ', 'ٓ', 'ٰ', 'ْ', 'ٌ', 'ٍ', 'ً', 'ّ', 'َ');
$string = str_replace($remove, '', $string);

echo $string; // outputs الحمد لله رب العالمين

What's important here is the $remove array. It looks weird because there's a combining character between the ' quotes, so it modifies one of those single quotes. This might need saving in the same character encoding as your text is.

like image 58
ljacqu Avatar answered Sep 22 '22 12:09

ljacqu


try this:

$string = 'الْحَمْدُ لِلَّهِ رَبِّ الْعَالَمِينَ';
$string = preg_replace("~[\x{064B}-\x{065B}]~u", "", $string);
echo $string; // outputs الحمد لله رب العالمين
like image 37
Hosein Shahrestani Avatar answered Sep 20 '22 12:09

Hosein Shahrestani