DEV Community

Hossein Sadeghi
Hossein Sadeghi

Posted on

PHP Create slugify URL from All language to English

PHP transliterator_transliterate - examples . These are the top rated real world PHP examples of transliterator_transliterate extracted from open source projects. You can rate examples to help us improve the quality of examples.

Programming Language: PHP

Method/Function: transliterator_transliterate

(PHP 5 >= 5.4.0, PHP 7, PECL intl >= 2.0.0)

...

📝Description

public Transliterator::transliterate ( string $subject [, int $start [, int $end ]] ) : string|false
Enter fullscreen mode Exit fullscreen mode

📝Parameters

transliterator

In the procedural version, either a Transliterator or a string from which a Transliterator can be built.
Enter fullscreen mode Exit fullscreen mode

subject

The string to be transformed.
Enter fullscreen mode Exit fullscreen mode

start

The start index (in UTF-16 code units) from which the string will start to be transformed, inclusive. Indexing starts at 0. The text before will be left as is.
Enter fullscreen mode Exit fullscreen mode

end

The end index (in UTF-16 code units) until which the string will be transformed, exclusive. Indexing starts at 0. The text after will be left as is.
Enter fullscreen mode Exit fullscreen mode

Code Example 1:

<?php

function slugify($string)
{
    $string = transliterator_transliterate("Any-Latin; Latin-ASCII; [\u0080-\u7fff] remove; Lower();", $string);
    $string = preg_replace('/[-\s]+/', '-', $string);
    return trim($string, '-');
}

echo '/n'.slugify('A æ Übérmensch på høyeste nivå! И я люблю PHP! fi'); 
//out: a-ae-ubermensch-pa-hoyeste-niva!-i-a-lublu-php!-fi


echo '/n'.slugify('وب فارسی در مجله دلگرم delgarm');
//out: wb-farsy-dr-mjlh-dlgrm-delgarm


echo '/n'.slugify('أصبح تسجيل طلاب الصف الأول إلكترونيًا');
//out: asbh-tsjyl-tlab-alsf-alawl-alktrwnyaa

?>

Enter fullscreen mode Exit fullscreen mode

function Example 2:

 public static function slugify($string, $separator = null)
 {
     $separator = null !== $separator ? $separator : (null !== self::$separator ? self::$separator : '-');
     $slug = trim(strip_tags($string));
     $slug = transliterator_transliterate('NFD; [:Nonspacing Mark:] Remove; NFC; Any-Latin; Latin-ASCII; Lower();', $slug);
     $slug = preg_replace("/[^a-zA-Z0-9\\/_|+ -]/", '', $slug);
     $slug = preg_replace("/[\\/_|+ -]+/", $separator, $slug);
     $slug = trim($slug, $separator);
     return $slug;
 }
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
yongdev profile image
Yong Dev

Nice Tutorial