Инструменты пользователя

Инструменты сайта


programming:php:friendly-url

SEO URL

    /**
    * Сконвертировать строку в URL френдли строку из латиницы
    * 
    * @param string $alias
    * @param mixed $tolower
    * @return string
    */
    function to_url($alias, $tolower = false){

        $cyr=array(
        "Щ", "Ш", "Ч", "Ц","Ю", "Я", "Ж", "А","Б","В","Г","Д","Е","Ё","З","И","Й","К","Л","М","Н", 
        "О","П","Р","С","Т","У","Ф","Х", "Ь","Ы","Ъ","Э","Є","Ї",
        "щ", "ш", "ч", "ц","ю", "я", "ж", "а","б","в","г","д","е","ё","з","и","й","к","л","м","н", 
        "о","п","р","с","т","у","ф","х", "ь","ы","ъ","э","є","ї","№");
        
        $lat=array(
        "Shh","Sh","Ch","C","Ju","Ja","Zh","A","B","V","G","D","Je","Jo","Z","I","J","K","L","M",
        "N","O","P","R","S","T","U","F","Kh","","Y", "`","E","Je","Ji",
        "shh","sh","ch","c","ju","ja","zh","a","b","v","g","d","je","jo","z","i","j","k","l","m",
        "n","o","p","r","s","t","u","f","kh","","y", "","e","je","ji",""
        );        
            
        for($i=0; $i<count($cyr); $i++){
            $c_cyr = $cyr[$i];
            $c_lat = $lat[$i];
            $alias = str_replace($c_cyr, $c_lat, $alias);
        }
        $alias = preg_replace("/([qwrtpsdfghklzxcvbnmQWRTPSDFGHKLZXCVBNM]+)[jJ]e/", "\${1}e", $alias);
        $alias = preg_replace("/([qwrtpsdfghklzxcvbnmQWRTPSDFGHKLZXCVBNM]+)[jJ]/", "\${1}", $alias);
        $alias = preg_replace("/([eyuioaEYUIOA]+)[Kk]h/", "\${1}h", $alias);
        $alias = preg_replace("/^kh/", "h", $alias);
        $alias = preg_replace("/^Kh/", "H", $alias);
            
            
        if ($tolower == true)
        {
            $alias = strtolower($alias);
        }
        $alias = preg_replace("/[^\w-\.]+/u", "-", $alias);
        $alias = trim($alias, '-');
        return $alias;
    }
programming/php/friendly-url.txt · Последнее изменение: 2017/03/27 11:26 — artur

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki