Generate SEO Friendly URL from String in PHP

SEO friendly URLs are designed in a way where the URL slug is optimized for SEO. The use of the SEO-friendly URL helps to improve the search engine ranking. Also, the search engine friendly URL gives an idea about the web page content. The difference between a normal URL and SEO friendly URL is given below.

  • Noraml URL: https://www.example.com/posts.php?id=123
  • SEO URL: https://www.example.com/php-tutorial-for-beginners

It clearly shows that the SEO URL is more human-friendly than the normal URL. From SEO URL the user can get a clear idea of what will be on the web page they are clicking. If you want to increase your website ranking on Search Engines and make it more user-friendly, you need to switch to an SEO-friendly URL. In this tutorial, we’ll show you how to generate SEO friendly URL from string using PHP.

This example script makes it easy to convert title string to SEO-friendly URL in PHP. For better usability, we will create a custom PHP function to generate a clean and SEO friendly URL slug from string.

In this tutorial, we’ll show you how to generate SEO friendly URL from string using PHP. Our example script makes it easy to convert title string to SEO friendly URL in PHP. We’ve grouped together all the PHP code into a function named generateSeoURL(). The generateSeoURL() function automatically create clean and SEO friendly URL slug from string.

Create SEO Friendly URL in PHP

The generateSeoURL() function automatically creates SEO friendly URL slug from the string using PHP. A title string needs to be passed as input and it returns a human-friendly URL string with a hyphen (-) as the word separator.

  • $string – Required. The string which you want to convert to the SEO friendly URL.
  • $wordLimit – Optional. Restrict words limit on SEO URL, default is 0 (no limit).
function generateSeoURL($string$wordLimit 0){ 
    
$separator '-';
    
    if(
$wordLimit != 0){
        
$wordArr explode(' '$string);
        
$string implode(' 'array_slice($wordArr0$wordLimit));
    }

    
$quoteSeparator preg_quote($separator'#');

    
$trans = array(
        
'&.+?;'                 => '',
        
'[^\w\d _-]'            => '',
        
'\s+'                   => $separator,
        
'('.$quoteSeparator.')+'=> $separator
    
);

    
$string strip_tags($string);
    foreach (
$trans as $key => $val){
        
$string preg_replace('#'.$key.'#iu'$val$string);
    }

    
$string strtolower($string);

    return 
trim(trim($string$separator));
}

Uses

Use the generateSeoURL() function to generate SEO friendly URL from title string using PHP. Provide the article title or string in the first parameter of generateSeoURL() function. If you want to restrict words on the URL, provide the number of the words otherwise can omit it.
Without Words Limit:

$postTitle 'Generate SEO Friendly URL from String in PHP'; 

$seoFriendlyURL generateSeoURL($postTitle);

// Output will be: generate-seo-friendly-url-from-string-in-php

With Words Limit:

$postTitle 'Generate SEO Friendly URL from String in PHP'; 

$seoFriendlyURL generateSeoURL($postTitle4);

// Output will be: generate-seo-friendly-url

Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request

5 Comments

  1. Pinky Said...
  2. Firoz Ansari Said...
  3. Rony Said...
  4. Dibyajyoti Das Said...
    • CodexWorld Said...

Leave a reply

keyboard_double_arrow_up