str_replace once

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
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

str_replace once

Post by superdezign »

I'm drawing a blank. I want str_replace to do only run through a string once. preg_replace has the 'limit' parameter, but str_replace does not. I'd like to avoid using preg_replace since I know the exact tring I'd like to replace, but the string can occur more than once.
User avatar
TheMoose
Forum Contributor
Posts: 351
Joined: Tue May 23, 2006 10:42 am

Post by TheMoose »

Just use preg_replace with the exact string and limit param? The pattern can be a pattern with no regex in it :)
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post by Gente »

I don't think it's a good idea to use preg_replace() here.
If you want to replace the first fragment you can just write simple function with combination of strpos(), strlen() and substr().
I no you can try to manipulate with explode() and join() :)
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Gente wrote:I don't think it's a good idea to use preg_replace() here.
If you want to replace the first fragment you can just write simple function with combination of strpos(), strlen() and substr().
I no you can try to manipulate with explode() and join() :)
The reason I was trying to avoid preg_replace was because str_replace is faster, but to go through that many functions sounds like preg_replace would be the better choice.
User avatar
Jaxolotl
Forum Contributor
Posts: 137
Joined: Mon Nov 13, 2006 4:19 am
Location: Argentina and Italy

Post by Jaxolotl »

what about substr_replace() http://php.net/substr_replace
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Jaxolotl wrote:what about substr_replace() http://php.net/substr_replace
I looked into that earlier as well, but it'd require a call to strpos().
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

superdezign wrote:
Jaxolotl wrote:what about substr_replace() http://php.net/substr_replace
I looked into that earlier as well, but it'd require a call to strpos().
I don't have anything backing me up on this, but shouldn't a substr_replace() combined with strpos() be faster than a preg_replace()-call?
User avatar
Jaxolotl
Forum Contributor
Posts: 137
Joined: Mon Nov 13, 2006 4:19 am
Location: Argentina and Italy

Post by Jaxolotl »

remember strpos() will return the position of the LAST occurence only
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

strrpos() finds the last occurence in the string. strpos() finds the first occurence.
Post Reply