Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP ereg vs. preg

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?

like image 942
Evernoob Avatar asked Sep 01 '09 09:09

Evernoob


People also ask

What is difference between Str_replace and Preg_replace?

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.

What does preg match do in PHP?

Definition and Usage The preg_match() function returns whether a match was found in a string.

What are the advantages of regular expression over inbuilt function?

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.

Is Preg_match case sensitive?

preg_match is case sensitive. A match. Add the letter "i" to the end of the pattern string to perform a case-insensitive match.


1 Answers

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.

like image 186
Sampson Avatar answered Sep 21 '22 20:09

Sampson