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.
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.
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($wordArr, 0, $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));
}
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($postTitle, 4);
// 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
Nice , helpful training module , thanks
How we can rewrite url using .htaccess or using your function thats you explain above.
How to implement it? can you please explain it in detail!!!
But how to actually implement/convert “posts.php?id=123” into your text-string in the URL section??
We’ll soon publish the tutorial on URL Rewrite topic, it will fulfill your requirement. Please subscribe our newsletter to get notified.