Dynamically generate slug in PHP

Votes : 5
826
You can generate your own folder or file name, SEO friendly url. but users give various name to your applications. So you need to dynamically generate function to reduce problem.
ඔබට folder or file වලට නමක් දීමට, SEO වලට සරිලන url path එකක් හදගන්න පුලුවන්. නමුත් ඔබ ලියන වැඩසටහන භාවිතාකරන පිරිස් විවිධ යෙදුම් භාවිතා කල හැක. මේ නිසා ඇතිවිය හැකි වැරැදි අවම කර folder or file වලට නමක් හෝ SEO වලට සරිලන url path හදාගන්න පුලුවන්.
function ESlug($str) {
    /*
     * Dynamically generate slug
     * @param (string|required) $str - string which is need to genarete slug. 
     * 
     * @return (string) slug
     * 
     * source : http://codrate.com/articles/dynamically-generate-slug-in-php
     */
    // transliterate to utf-8
    $str = utf8_encode($str);     
    //Non letter or digits  replaced  by -
    $str = preg_replace('~[^\pL\d]+~u', '-', $str);
    // transliterate to utf-8
    $str = iconv('utf-8', 'us-ascii//TRANSLIT', $str);
    // remove unwanted characters
    $str = preg_replace('~[^-\w]+~', '', $str);
    // trim  first and last charactors
    $str = trim($str, '-');
    // remove duplicate -
    $str = preg_replace('~-+~', '-', $str);
    // lowercase
    $str = strtolower($str);
    if (empty($str) || trim($str) == '') {
        $str = '';
    } 
    return $str;
}
asked 4 months,13 days ago

What is the codrate ?

codrate.com is a standard, fast cross browsing and highly versatile site. It is useful for many large number of Program Development Industries. So you can get support form Codrators , who are the codrate's joiners around world to help your program developments, You can answer other codrator's questions. Communicate with them. Share your knowledge with them. Do you have an interest in programming, So publish your articles about programming. It will help to maintain your professional co-profile. Actually codrate.com is not such as a regular web site. It will be gave new experience, best narrow cross-browser view, reduce processing time to receive browsing request, it's mean do not wasting your time to browsing codrate's web pages because it has been upgrade always modern coding ways. So, what do you waiting for ?. Try your own.

Copyright 2015 Pride - Company. Design by Esila