change characters for url?

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
User avatar
potato
Forum Contributor
Posts: 192
Joined: Tue Mar 16, 2004 8:30 am
Location: my lovely trailer, next to the big tree

change characters for url?

Post by potato »

Hi everybody,

long time no see... :)

i came on the problem that when my artists sign up, i want to give them a url like:
http://www.mysite.com/bands/artistname

For that, no problem.
But... When the artistname is for example:
http://www.mysite.com/bands/more words artistname

I would like that all the spaces in the artistname are replaced with underscores, and that
characters that arent valid in a url are deleted.
Whit what function i can do that?

Any help would be great...

Greets,
Tom
tmarion
Forum Newbie
Posts: 5
Joined: Sun Feb 26, 2006 3:23 am
Contact:

Post by tmarion »

Look at preg_replace or str_replace.

If you prefer regular expressions, use the first option otherwise use the second.

Thanks,
Tony
User avatar
potato
Forum Contributor
Posts: 192
Joined: Tue Mar 16, 2004 8:30 am
Location: my lovely trailer, next to the big tree

Post by potato »

Ok, i found it!

here it is:

Code: Select all

$string = "here comes the string...";
$string = ereg_replace("[^A-Z, a-z,&]", "", $string);
$string = ereg_replace("[^A-Z,a-z,&]", "_", $string);
echo $string;

//will output: here_comes_the_string
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you want to exclude commas?
Post Reply