string operation to replace a character

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
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

string operation to replace a character

Post by susrisha »

i have a string that goes like

Code: Select all

 
$somestring = 'laks:asdasd:hhsah:asdhsau';
 
 
I want to replace the character ':' with '-' , so my out put should be

Code: Select all

 
 
$converted_string = 'laks-asdasd-hhsah-asdhsau';
 
how do i do it??
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: string operation to replace a character

Post by omniuni »

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Look at http://us2.php.net/str_replace

If you want, create a function to do it specifically, something along the lines of

Code: Select all

 
function replacecolons($mystring){
$mynewstring = str_replace(':', '-', $mystring );
return $mynewstring;
}
 
//call your function...
$gibberish = 'blahblah:blah';
$gibberish = replacecolons($gibberish);
//$gibberish now is blahblah-blah
 
I think that should work, although I don't have time to test it at the moment.

-OmniUni


~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: string operation to replace a character

Post by susrisha »

thanks it works..
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: string operation to replace a character

Post by pickle »

Why bother with the extra replacecolons() function? There's no reason not to just call str_replace() directly.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: string operation to replace a character

Post by omniuni »

It offers flexibility and less typing if you need to expand it later, say, to deal with semicolons as well. It can make code easier to read, by providing a more "English" command than having to interpret each time. If you decide to change what it does, make one change in the function, and it is reflected throughout the program, rather than having to modify every instance of a command.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: string operation to replace a character

Post by pickle »

Valid opinion I guess. To me though, it seems like he very specifically wants to just replace colons & adding another function just creates more overhead.

Obviously though, it's up to the original poster.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: string operation to replace a character

Post by omniuni »

The overhead is a good point. For me, at least, I try to do reusable code when I can simply because I'm not doing a lot of complicated things yet, and the overhead is far less of a concern to me than the fact that I often change my mind later about how I want to work with X Y or Z.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: string operation to replace a character

Post by pickle »

I've got 30+ inter-campus apps running on one server, so I tend (or try to be at least) a bit of an overhead nazi ;)
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply