Create SEO Friendly URL Using PHP

In this article, I will provide you php method script to generate SEO-friendly URL for web applications using PHP.

We will discuss how to generate SEO friendly url using PHP, Now days we are using query strings in web url to pass information between your web pages.

In this article, we will learn how to generate SEO-friendly URLs from query string values.

Query string attached parameters onto end of URL after the question mark. We have defined the function to get the Search Engine Friendly Page URL from the given string.

Create SEO-Friendly Slug URL Using PHP

Let’s create a seoUrl() method that will first remove HTML tags if any are found, Then we’ll remove special characters, trim the white space.

Finally, We will replace whitespace with hyphen and convert a string into lower characters.

/**
Description: Prepare SEO Friendly URL
result: SEO Frinedly URL out by replacing spaces and special characters with slash(/)
*/
function seoUrl()
{
$PageTitle="id=1 & name='parvez'";
// Remove HTML Tags if found
$string=strip_tags($string);
// Replace special characters with white space
$string=preg_replace('/[^A-Za-z0-9-]+/', ' ', $string);
// Trim White Spaces and both sides
$string=trim($string);
// Replace whitespaces with Hyphen (-)
$string=preg_replace('/[^A-Za-z0-9-]+/','/', $string);
// Conver final string to lowercase
$slug=strtolower($string);
echo $slug;
}
// result:
id/1/name/parvez

One thought on “Create SEO Friendly URL Using PHP

Leave a Reply

Your email address will not be published.