Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Slug in PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 380
    Comment on it

    Hi All,

    Slug :- It is a part of the URL to makes the URL more user-friendly. There are also many other advantages of the Slug. Firstly, search engines such as Google will rank your page higher for the keywords contained in the URL Secondly, usability is another reason, if you receive that link, you know what to expect.

    Slug is a very good concept it helps your site to get good ranking on search engines following is the code to create slug for your website in core PHP.

    Syntax:-


    //Where $content is the string contains the content displayed on the webpage but remember to trim the string to a predefined limit. 
    $slug = createSlug($content);
    
    function createSlug($content) {
             $content = preg_replace('~[^\\pL\d]+~u', '-', $content);
             $content = trim($content, '-');
             $content = iconv('utf-8', 'us-ascii//TRANSLIT', $content);
             $content = strtolower($content);
             $content = preg_replace('~[^-\w]+~', '', $content);
              if (empty($content)) {
                return 'n-a';
            }
             return $content;
        }
    

    While in Laravel 5.0 we have a in built-in function for slug i.e. str_slug() and it's syntax is mentioned below:-

    $slug = str_slug($text);

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: