...

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



treeleaf20
02-23-2012, 08:46 PM
All,
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?

angst
02-23-2012, 09:00 PM
try this;


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

treeleaf20
02-23-2012, 09:17 PM
That did the trick.
Thanks

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

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

Keleth
02-23-2012, 10: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