How to create SEO URLs from String in PHP

How to create SEO URLs from String in PHP

URL stands for Uniform Resource Locator.URLs or web address used to locate exact information page on the web like http://blog.webcomers.com/how-to-create it’s a URL , where how-to-create is  created from a string and in this tutorial I will give you a method to know about that function of PHP. This is a very basic solution for PHP Developers.

Here is the code snippet to create URLs from a String.

Step 1: It is very simple just create the function with the name prety_url as shown below in the code.

<?php
function prety_url($str){
if($str !== mb_convert_encoding( mb_convert_encoding($str, ‘UTF-32’, ‘UTF-8’), ‘UTF-8’, ‘UTF-32’) )
$str = mb_convert_encoding($str, ‘UTF-8’, mb_detect_encoding($str));
$str = htmlentities($str, ENT_NOQUOTES, ‘UTF-8’);
$str = preg_replace(‘`&([a-z]{1,2})(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig);`i’, ‘\1’, $str);
$str = html_entity_decode($str, ENT_NOQUOTES, ‘UTF-8’);
$str = preg_replace(array(‘`[^a-z0-9]`i’,’`[-]+`’), ‘-‘, $str);
$str = strtolower( trim($str, ‘-‘) );
return $str;
}

?>

Step 2: Now call the function with passing the string  parameter.

<?php
$str = “How to Create OPTIMIZE URL”; // this string will be submitted in parameter.
echo prety_url($str); // show optimized url string.
?>

Output: After calling the function the string(How to Create OPTIMIZE URL) will be converted to how-to-create-optimize-url

This function first of all convert string to utf-8 after that use perg_replace and change white spaces to “-” and remove special characters.

These URLs also make good impact on your SEO and recommended by Google.

I hope it will be helpful for web developers..