View Full Version : Convert string into URL friendly string with PHP

02-23-2012, 07:46 PM
I'm trying to make a URL friendly string. So whatever the string is, I'm trying to make it lowercase and add a dash inbetween the characters. So it would look something like this:

This is a test

This would be converted to this-is-a-test

How can I do that with PHP?

02-23-2012, 08:00 PM
try this;

$String = "This is a test";
$String = str_replace(" ", "-", strtolower($String));
echo $String;

02-23-2012, 08:17 PM
That did the trick.

02-23-2012, 08:30 PM
You could also look at urlencode

02-23-2012, 08:31 PM
urlencode() wouldn't give him the result he asked for.

02-23-2012, 09:54 PM
urlencode() wouldn't give him the result he asked for.

Sorry, my response as poor.

urlencode() will encode it into a format a URL can take. It won't be replacing spaces with dashes, but it accomplishes the same thing in a more standard format.

If you specifically want to switch spaces with dashes, the above is the best method. But if you are looking for a standardized way of encoding a string for URL, urlencode() is the way to go.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum