replace whitespace with a hyphen

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

replace whitespace with a hyphen

Post by ianhull »

Hi Guys,

I am wondoering how to replace whitespace with a dynamic striing to a hyphen(-)

For example, the end user might type, Red Curtains,

I need to translate this into red-curtains.html

please help.

Thanks
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

You want to manipulate string, take a look at the string functions of php at http://de3.php.net/manual/en/ref.strings.php
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post by ianhull »

Thanks Volka,

I am still a bit confused though, as I never know what position the white space will be in.

Any help or examples would be vey much appreciated.
Thanks
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Post by mickd »

More specifically you can look into str_replace from volka's link.

Also, if you need, strtolower or not, here it is just incase.
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post by ianhull »

Code: Select all


<?php
$string = "Red Curtains";
$newstring = str_replace(" ", "-", $string );
echo $newstring;
?>

This does the job
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post by ianhull »

Yes thanks mickd

I did need to convert to lower case too :)

Thanks guys
Post Reply