Categories: PHP Tips

How to make SEO friendly URLs from any string with PHP?

I wanted to convert very complicated strings in to SEO friendly URLs. The challenge was to generate a unified URL after removing all illegal characters in the string.

As I roam through internet I could find the following cool function which returns a nice URL no matter what we through at it. I modified the function a little bit.

setlocale(LC_ALL, 'en_US.UTF8'); //Set locale to English for all of the below

function BuildUrl($str, $delimiter='-', $replace=array()) {
if( !empty($replace) ) {
$str = str_replace((array)$replace, ' ', $str);
}
$clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
$clean = strtolower(trim($clean, '-'));
$clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);

return $clean;
}

Just call the function with your string as it’s parameter.

       
            $clean_url=BuildUrl($string);


Optional Settings

By default all the spaces and illegal blocks will be separated by a hyphen (-) but if you prefer to use a different character just define your character in the second parameter.

Eg. this-is-my-clean-url vs this_is_my_clean_url

           $clean_url=BuildUrl($string, "_");

In an advance situation if your string contains some characters which should be converted to a legal character or character set rather than removing them from the URL define your if then list as an array like below and define it as the third parameter.

   $replace = array(
'illegal' => array('/ä/', '/ö/', '/ü/', '/Â/', '/é/'),
'legal' => array('ae', 'oe', 'ue', 'Aa', 'e')
);

$clean_url=BuildUrl($string, "_", $replace);

Hope this will be helpful to your situation :).

Cheers!

Recent Posts

How do I create an engaging and informative online quiz or assessment?

Creating an engaging and informative online quiz or assessment can be a powerful tool for… Read More

1 year ago

What are the most effective methods for managing and reducing work-related stress in the hospitality industry?

Work-related stress is a common issue in the hospitality industry, where employees often face long… Read More

1 year ago

How can I improve my assertiveness and communication skills in a leadership position?

In a leadership position, assertiveness and effective communication skills are crucial for success. Being able… Read More

1 year ago

What are the key elements of a successful employee recognition and rewards program?

Employee recognition and rewards programs play a crucial role in motivating and engaging employees, as… Read More

1 year ago

How do I effectively manage and respond to customer feedback and reviews?

Customer feedback and online reviews play a crucial role in shaping a company's reputation and… Read More

1 year ago

What are the best strategies for effective time management as a stay-at-home parent?

Effective time management is crucial for stay-at-home parents who juggle multiple responsibilities on a daily… Read More

1 year ago