Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string replacement question.

Status
Not open for further replies.

seahorse123

Programmer
Jul 25, 2005
39
0
0
US
Any comments on the function:
preg_replace(), reg_replace() and str_replace()
what's the difference between them? which one is more often used?
 
First, there is no function called reg_replace(). It's ereg_replace().

str_replace() does not use regular expressions. It's a simple string-for-string replacement.

ereg_replace() and pre_replace() both use something called regular expressions. A regular expression [link to wikipdia article] does not match an exact string -- it describes in a general way a pattern within a string and attempts to match that. So str_replace() can match exactly "999", where ereg_replace() and preg_replace() can both match "any three numerical digits".

The difference between ereg_replace() and preg_replace() is the regular_expression engines each function uses. preg_replace() uses a perl-compatible regular expression engine and according to the PHP online manual is often faster than ereg_replace(). (see the tip here). But str_replace(), since it does a simpler function, will be less resource-intensive than either of the other two.

As to which to use, there is no single universally appropriate function. If you need to do a simple string replacement use str_replace() as it's less resource-intensive. If you need to match a complex string that could vary but can be described in a general way, then use one of the regular expression functions. (I prefer preg_replace(), but others like ereg_replace()).


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top