...

View Full Version : Resolved Function to convert MYSQL results to SEO-friendly URL's



squidnunc
09-14-2009, 04:42 PM
Hi all,

I'm just learning php and I've created a php function to clean up the results of a mysql query and convert the result to an SEO-friendly url. This function works fine but is probably a lot more clumsy and cumbersome than it needs to be and I am hoping someone can help me clean it up a little.

The function can take titles like these:

Los Angeles, CA 8/9/09
Who am I?
(Just) Crazy

and turn them into url's like these:

los-angeles-ca-8-9-09
who-am-i
just-crazy



<?php
// this function removes all non-alphanumeric characters from a url, adds hyphens between words, makes all letters lowercase
function cleanurl($string)
{
// replaces anything not a letter, number, or a space with one white space
$cleanurl = preg_replace('/[^A-Za-z0-9\s]/i', ' ', $string);
// eliminates extra white spaces created above
$cleanurl = preg_replace('/\s\s+/', ' ', $cleanurl);
// replaces white space with a hyphen
$cleanurl = str_replace(' ', '-', $cleanurl);
// removes any hyphen from beginning of string
$cleanurl = preg_replace('/^-/', '', $cleanurl);
// removes any hyphen from end of string
$cleanurl = preg_replace('/-$/', '', $cleanurl);
// makes all letters lower case
$cleanurl = strtolower($cleanurl);
// returns the clean url to the page
return $cleanurl;
}
?>


As you can see, it's taking me many separate progressions to get the desired result. There must be a more straightforward and less resource-intensive way to accomplish this.

Thanks,
Keith

bacterozoid
09-14-2009, 05:55 PM
Here's an example from the PHP documentation:


<?php
$string = 'The quick brown fox jumped over the lazy dog.';
$patterns[0] = '/quick/';
$patterns[1] = '/brown/';
$patterns[2] = '/fox/';
$replacements[2] = 'bear';
$replacements[1] = 'black';
$replacements[0] = 'slow';
echo preg_replace($patterns, $replacements, $string);
?>

You can use arrays as your arguments to preg_replace to simplify it a little bit.

squidnunc
09-14-2009, 06:09 PM
Thank you very much for responding, bacterozoid. Would the example you suggested using just pretty-up the code or actually make the function less resource-intensive?

Also, the function I wrote had to work in the order it was written or it would not perform the same. Would the example you suggested maintain the proper progression of replaces?

bacterozoid
09-14-2009, 06:18 PM
squid,

It should speed up the code, but I doubt you will notice. You're calling preg_replace fewer times, is all. It still has to do all the replacement work.

It will also work in the proper order, so long as you fill your arrays in the same order. $array[0] should be the first replacement, $array[1] the second, and so on.

squidnunc
09-14-2009, 06:57 PM
Based on the example you provided, I rewrote the function as follows:



<?php
// this function removes all non-alphanumeric characters from a url, adds hyphens between words, makes all letters lowercase
function cleanurl($string)
{
$patterns[0] = '/[^A-Za-z0-9\s]/';
$patterns[1] = '/\s\s+/';
$patterns[2] = '/\s/';
$patterns[3] = '/^-/';
$patterns[4] = '/-$/';
$replacements[0] = ' ';
$replacements[1] = ' ';
$replacements[2] = '-';
$replacements[3] = '';
$replacements[4] = '';
$cleanurl = preg_replace($patterns, $replacements, $string);
$cleanurl = strtolower($cleanurl);
return $cleanurl;
}
?>


The function as rewritten still works fine and it seems to be more elegant code. This is the first php function I have written. Does this seem to be the best way to accomplish my goal and would you consider this to be a useful function? Thanks for all your help, bacterozoid!

bacterozoid
09-14-2009, 07:08 PM
It looks pretty good to me. The only thing I would change is to do strtolower() first, then you can change your first regular expression and remove uppercase letters.

If you prefer, you could also initialize your arrays like this:


$replacements = Array('value', 'value', 'value');

It just makes your code look different. In this case, I would do that because it's my personal preference to squeeze more crap on the same line (not because it makes the code any better). If you prefer your way and the method in the example I provided, that's perfectly acceptable as well.

Good luck!

squidnunc
09-14-2009, 07:30 PM
Using your latest suggestion, I've changed to the following code:



<?php
function cleanurl($string)
{
$cleanurl = strtolower($string);
$patterns = array('/[^a-z0-9\s]/', '/\s\s+/', '/\s/', '/^-/', '/-$/');
$replacements = array(' ', ' ', '-', '', '');
$cleanurl = preg_replace($patterns, $replacements, $cleanurl);
return $cleanurl;
}
?>


It still works and it makes for a smaller but slightly harder to read function. I like it, though, because I too like to have as few lines as possible which is why I asked this question to begin with. I believe this is now as concise as possible. Thanks again, bacterozoid!

Zaighster
03-29-2011, 02:52 AM
I know this is an old and resolved thread, but I thought that anyone stumbling across this might like even more concise code


$cleanurl = strtolower($string);
$cleanurl = preg_replace('/[^a-z0-9]+/', '-', $cleanurl);
$cleanurl = trim($cleanurl, '-');

The preg_replace replaces any sequence of one or more non-alphanumeric chars with a single hyphen, merging the first three steps of the algorithm. The trim removes any hyphens at the beginning or end of the string, replacing the last two steps. To put it all in a single line the function could look like this


function cleanurl($string, $join = '-')
{
return trim(preg_replace('/[^a-z0-9]+/', $join, strtolower($string)),$join);
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum