×

Please Login or Register to continue.

3
(2K Views)

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. 

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

Share

1 Answer
(3.4K Points)
(edited)
1

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.

<?php 
 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 - 

كيفية-تحويل-الكلمات-العربية-إلى-سبيكة-في-php

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() . 


Comment

Related Questions :-

Featured Items:-


Certificate system in PHP website | PHP scripts

$22



Home Credito loan finance management multipurpose system in PHP

$65



Result management system with Marksheet in PHP website | PHP Scripts

$39




Your Answer

You can post your answer after login..