• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

preformance, str_replace vs. preg_replace

mattsoftnet

New Member
I want to squeeze out every bit of preformance out of the template system I'm making in php. what would give me better preformance, always using preg_replace and other regular expression functions, or using preg_replace when needed, but str_replace when I don't need the extra regular expression power? I did some simple tests just now and found some unexpected results. I found preg_replace actually runs a bit faster then str_replace when they're both doing just a simple 1 char string replace. what do you think?
 
str_replace is suppose to be faster as it won't require the load of regular expressions that preg_replace would have.
 
kabatak said:
str_replace is suppose to be faster as it won't require the load of regular expressions that preg_replace would have.

I'm thinking maybe preg_replace was preforming better for me because once it processes a regular expression once it caches it. I was replacing the same thing over and over. str_replace is probably better for doing simpler replaces. thanks for your input.
 
mattsoftnet said:
I'm thinking maybe preg_replace was preforming better for me because once it processes a regular expression once it caches it. I was replacing the same thing over and over. str_replace is probably better for doing simpler replaces. thanks for your input.

Depending on how you set up your script before or after, preg_replace doesn't do anything to save what it has already done or will do. str_replace doesn't either. Also, str_replace, as the other user said would be better I guess, because it doesn't need to go through and process the Regular Expressions.

str_replace("find this", "change", value_of);

where value_of is the variable that is being changed/found/etc.
 
Back
Top