DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Nima Sadeghi
Nima 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
Prosper Ugbovo

Nice Tutorial

12 Gorgeous UI Components for Your Design Inspiration

>> Check out this classic DEV post <<