A slug is a part of a URL that identifies a specific page on a website in a human-readable and SEO-friendly way. For example, the slug for this article is "how-to-generate-slug-of-title-from-any-language-php". A slug usually consists of lowercase letters, numbers, and hyphens, and it should reflect the main keywords and topic of the page.
Generating a slug from a title in PHP can be done in several steps, such as:
• Converting the title to lowercase using the strtolower function.
• Removing any leading or trailing spaces using the trim function.
• Replacing any non-alphanumeric characters or spaces with hyphens using the preg_replace function and a regular expression.
• Removing any duplicate hyphens using the preg_replace function and another regular expression.
However, this method may not work well for titles that contain characters from other languages, such as Chinese, Arabic, or Russian. These characters may not be converted correctly to lowercase, or they may be removed entirely by the regular expression. This can result in an inaccurate or empty slug that does not match the title or the content of the page.
To solve this problem, we can use a PHP extension called intl, which provides various functions for internationalization and localization. One of these functions is transliterator_transliterate, which can convert any string from one script or writing system to another. For example, we can use this function to transliterate Chinese characters to Latin characters, or Arabic characters to ASCII characters.
Using this function, we can modify our slug generation method as follows:
• Transliterating the title to ASCII using the transliterator_transliterate function with the "Any-Latin; Latin-ASCII" option.
• Converting the title to lowercase using the strtolower function.
• Removing any leading or trailing spaces using the trim function.
• Replacing any non-alphanumeric characters or spaces with hyphens using the preg_replace function and a regular expression.
• Removing any duplicate hyphens using the preg_replace function and another regular expression.
This method should work for any title in any language, as long as the intl extension is installed and enabled on your PHP server. You can check if the extension is available using the extension_loaded function with the "intl" parameter.
Here is an example of how to use this method in CakePHP code:
If you want to like above result check and use below function. I am writing on CAKEPHP framework. so you will see CakeException, you can change it for throw or any exit from php command.
Explore My Other Channel for More Cool and Valuable Insights
π Youtube Learn Tech Tipsπ Tiktok
π Facebook:
public static function transliterate( $string, $transliterator = null)
{
$_defaultTransliteratorId = 'Any-Latin; Latin-ASCII; [\u0080-\u7fff] remove';
if (empty($transliterator)) {
$transliterator = $_defaultTransliteratorId;
}
$return = transliterator_transliterate($transliterator, $string);
if ($return === false) {
throw new CakeException(sprintf('Unable to transliterate string: %s', $string));
}
return $return;
}
public function gen_slug( $string, $options = [])
{
if (is_string($options)) {
$options = ['replacement' => $options];
}
$options += [
'replacement' => '-',
'transliteratorId' => null,
'preserve' => null,
];
if ($options['transliteratorId'] !== false) {
$string = $this->transliterate($string, $options['transliteratorId']);
}
$regex = '^\p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}';
if ($options['preserve']) {
$regex .= preg_quote($options['preserve'], '/');
}
$quotedReplacement = preg_quote((string)$options['replacement'], '/');
$map = [
'/[' . $regex . ']/mu' => $options['replacement'],
sprintf('/^[%s]+|[%s]+$/', $quotedReplacement, $quotedReplacement) => '',
];
if (is_string($options['replacement']) && strlen($options['replacement']) > 0) {
$map[sprintf('/[%s]+/mu', $quotedReplacement)] = $options['replacement'];
}
$string = preg_replace(array_keys($map), $map, $string);
return $string;
}
And how to use it? Here is the answer π
$slug = "how are you friend?";
pr ($this->slug($slug));
$slug = "bαΊ‘n khoαΊ» khΓ΄ng";
pr ($this->slug($slug));
$slug = "η₯δ½ ζε";
pr ($this->slug($slug));
I hope this blog post helps you with generating slug of title from any language in CakePHP
#slug #php #url #seo #intl #transliteration #regex #lowercase #hyphen #ascii