Creating SEO URL from user input in PHP

How to create a SEO URL from user input, for web 2.0 style sites which use Apache's mod rewrite

<?php
function make_seo_title($str)
{
	// html decode, in case it is coming encoded (AJAX request)
	$seo_title = rawurldecode($str);
	// some characters that might create trouble
	$switch_chars = '(,),\,/';
	$sc = explode(',', $switch_chars);
	foreach ($sc as $c) $seo_title = str_replace($c, '-', $seo_title);
	// leave only alphanumeric characters and replace spaces with hyphens
	$seo_title = strtolower(str_replace('--', '-', preg_replace('/[\s]/', '-', preg_replace('/[^[:alnum:]\s-]+/', '', $str))));
	$len = strlen($seo_title);
	if ($seo_title[$len - 1] == '-') $seo_title = substr($seo_title, 0, -1);
	if ($seo_title[0] == '-') $seo_title = substr($seo_title, 1, $len);
	return $seo_title;
}
?>

Usage

Mostly used with MVC patterns in PHP this is a simple function to generate a search engine friendly, aka SEO, URL from an article page by using the input title field for example. Pass the value of the field you will be using as the title.


Comments

guest
Posted on 18.09.2011 12:46

nice....

guest
Posted on 03.07.2011 15:26

Hi,

I borrowed this to use in my own code and found a few places where it could be improved. I've pasted the updated code below.

Specifically, the changes are:

- I ensure that the provided value is scalar and if so I convert to a string. If an object, array, reference, or other non-scalar value is provided I throw an exception. In this version of the code I just return false as many developers are not yet using exceptions for cases where an illegal operation is attempted.

- I do not rawurldecode(). The burden to ensure that GET/POST/COOKIE data is in the proper format is left to another part of the system entirely. While I will check data types (as seen above), I do not like to clean data in de-centralized locations except where the method itself is created specifically for the task of cleaning data and returning the cleaned value. It can lead to code that either encodes or decodes a value too many times and the result is bad/wrong data due to over-manipulation.

- The switch_chars part is a bit inefficient. It just splits a string into an array. Better to just hard-code the array.

- str_replace() and str_ireplace() can both accept arrays for their first and second arguments. Rather than loop over the replacements array in PHP code, let the PHP engine do this at a lower level of C/C++ code. The loop is much more efficient and the code is just cleaner and easier to read/write.

- trim() accepts a string of characters as second argument. When supplied with the second argument, it trims any characters contained in the supplied string rather than the default behavior of trimming whitespace. This is a cleaner way to remove leading and trailing hyphen characters.

-------------------------------------

Enjoy!

-------------------------------------

public function makeSeoName($value) {
// Only attempt to modify scalar values (numbers, strings, boolean)
if(!is_scalar($value)) {
return false;
}
$value = (string) $value;
// Some characters that might create trouble
$troubleCharacters = array('(', ')', '\\', '/', '--');
$seoName = str_replace($troubleCharacters, '-', $value);

// Leave only alphanumeric characters and replace spaces with hyphens
$seoName = strtolower(preg_replace('/[\s]/', '-', preg_replace('/[^[:alnum:]\s-]+/', '', $seoName)));

// Trim it so that we don't have hyphens at either end of the seo name
return trim($seoName, '-');
}

guest
Posted on 20.11.2010 03:56

how to use it?~

guest
Posted on 04.10.2010 21:28

Exactly what I was looking for. Thank you!

Add your comment