Please Login or Register to continue.


Hello Support Team, 

I am working on an Arabic language website using PHP and MYSQL database.I want to convert Arabic words into a slug in PHP. This is important to have a slug. I made a function for Arabic words which is used Arabic words to slug in PHP. 

function slug($string) {
  $regex = "/\.$/";          
$regex = "/\.+$/";       
$regex = "/[.*?!@#$&-_ ]+$/";
$result = preg_replace($regex, "", $string);
    $val= preg_replace('/\s+/u', '-', trim($result));
    return strtolower($val); 
echo slug('كيفية تحويل الكلمات العربية إلى سبيكة في | PHP?');

It doesn't give perfect output. Is there any solution to make a perfect output of the Arabic language's words?

(4.3K Points)
in PHP


1 Answer
(3.4K Points)

I've written an answer for Hindi words to slug using PHP. You can use the same function to create your Arabic language slug in PHP. I've explained there - 

How to convert the Arabic language Title to Slug in PHP? 

1. First of all, save the Arabic title or you can say Arabic language string in a variable. 

2. Now, remove the beginning and last spaces from the string using the PHP trim() function. It removes the first and last unwanted spaces from the Arabic language string. 

3. We can use English words with an Arabic language title. In this step, you have to convert the string to lower Use strtolower() function to convert lower. 

4. Use Arabic language words in PHP preg_replace() function with numbers (0-9) , English characters (a-z), and Arabic words - 


This is the main part of the Arabic slug. It removes unwanted special characters and only adds matched Arabic and English words. 

5. Add minus - separator between words.

 function Arabic_slug($string) 
$string = trim($string);$string=strtolower($string);
$string =preg_replace("/[^a-z0-9_ءاأإآؤئبتثجحخدذرزسشصضطظعغفقكلمنهويةى\s-]/u", "", $string);
$string = preg_replace("/[\s-]+/", " ", $string);
$string = preg_replace("/[\s]/", '-', $string);
return $string ;
 $title="كيفية تحويل الكلمات العربية إلى سبيكة في | PHP?"; 
echo  Arabic_slug($title)

Output - 


The slug function is ready for Arabic and English words. If you want Arabic language words only then remove a-z from PHP preg_replace() 

Example - 

$string =preg_replace("/[^0-9_ءاأإآؤئبتثجحخدذرزسشصضطظعغفقكلمنهويةى\s-]/u", "", $string);

Output - 


Use the Arabic_slug() function to convert Arabic language title to slug() . 


Related Questions :-

Featured Items:-

Certificate system in PHP website | PHP scripts


Home Credito loan finance management multipurpose system in PHP


Result management system with Marksheet in PHP website | PHP Scripts


Your Answer

You can post your answer after login..