str_replace once
Moderator: General Moderators
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
str_replace once
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.
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
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.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()
- Jaxolotl
- Forum Contributor
- Posts: 137
- Joined: Mon Nov 13, 2006 4:19 am
- Location: Argentina and Italy
what about substr_replace() http://php.net/substr_replace
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
I looked into that earlier as well, but it'd require a call to strpos().Jaxolotl wrote:what about substr_replace() http://php.net/substr_replace
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?superdezign wrote:I looked into that earlier as well, but it'd require a call to strpos().Jaxolotl wrote:what about substr_replace() http://php.net/substr_replace
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA