I have noticed in the PHP regex library there is a choice between ereg and preg. What is the difference? Is one faster than the other and if so, why isn't the slower one deprecated?
Are there any situations where it is better to use one over the other?
str_replace replaces a specific occurrence of a string, for instance "foo" will only match and replace that: "foo". preg_replace will do regular expression matching, for instance "/f. {2}/" will match and replace "foo", but also "fey", "fir", "fox", "f12", etc.
Definition and Usage The preg_match() function returns whether a match was found in a string.
Below some advantages and uses of regular expressions are given: Regular expression helps the programmers to validate text string. It offers a powerful tool to analyze and search a pattern as well as to modify the text string. By using regexes functions, simple and easy solutions are provided to identify the patterns.
preg_match is case sensitive. A match. Add the letter "i" to the end of the pattern string to perform a case-insensitive match.
Visiting php.net/ereg displays the following:
Warning
This function has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 6.0.0. Relying on this feature is highly discouraged.
Down the page just a bit further and we read this:
Note: preg_match(), which uses a Perl-compatible regular expression syntax, is often a faster alternative to ereg().
Note my emphasis.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With