How to create slug for Hindi language in PHP?

4
(121 Views)

×

Please Login or Register to process.

I am creating a blog using PHP and MySQL database. I want to convert the post title to Hindi language slug. I am creating blog CMS in the Hindi language. I know the Hindi language very well. I am trying to create a Hindi language slug as well as an English language slug in the same blog.  Now, I just wanted to know that how can I create a Hindi language slug for blog posts.

asked 16 Nov in PHP by arnabisaha(2.1K Points)

Share

2 Answers
answered 20 Nov by vishalrana(3.2K Points) (edited)
4

Hindi language words are available on the internet. You can use all Hindi words with the PHP preg_replace() function. I know you want to convert the Hindi title to slug using the Hindi language. If you want to learn that how to create SLUG in the Hindi language then you should follow the points below. 

How to convert Hindi Title to Slug using PHP? 

1. First of all, store your Hindi title into a variable. 

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

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

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

(_ोौेैा्ीिीूुंःअआइईउऊएऐओऔकखगघचछजझञटठडढतथदधनपफबभमयरलवसशषहश्रक्षटठडढङणनऋड़ )

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

5. Add minus - separator between words 

<?php 
 function 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="हिंदी टाइटल का slug php के द्वारा कैसे बनाये। "; 
echo slug($title)
?>

Output - 

हिंदी-टाइटल-का-slug-php-के-द्वारा-कैसे-बनाये

Slug function is ready for Hindi and English words. If you want Hindi words only then remove a-z from PHP preg_replace() 

Example - 

$string =preg_replace("/[^0-9_ोौेैा्ीिीूुंःअआइईउऊएऐओऔकखगघचछजझञटठडढतथदधनपफबभमयरलवसशषहश्रक्षटठडढङणनऋड़\s-]/u", "", $string);

Output - 

हिंदी-टाइटल-का-के-द्वारा-कैसे-बनाये

It will produce only Hindi slug. 

 

 


Comment
It works for Hindi and English words slug. Thank you!
commented 6 days ago by arnabisaha

answered 3 days ago by priyasharma4005(4.1K Points) (edited)
1

Hindi slug is more important for Hindi writers. The slug plays an important role in SEO (Search engine optimization). You can convert Hindi words to Slug (Hindi words separated by commas). To convert Title to slug, you should use a function. 

<?php
function Hindi_slug($text,$seprator='-') {
  $regex = "/\.$/";          
$regex = "/\.+$/";       
$regex = "/[.*?!@#$&-_ ]+$/";
$result = preg_replace($regex, "", $text);
    $val= preg_replace('/\s+/u', $seprator, trim($result));
    return strtolower($val); 
}

I am not sure that It will give you the exact result but you can use more regex expressions for the Hindi language. 

 


Comment

Related Questions :-

Your Answer

You can post your answer after login..