Reputation: 27
Can anybody tell me the main difference between split and preg_split in php? As both functions works on regular expressions.
Thanks
Upvotes: 2
Views: 1778
Reputation:
split() uses POSIX as it's regex extension while preg_split() uses PCRE(Perl Compatible Regular Expression) as it's regex extension. PCRE has extended regex features.
Another main difference is that split is case insensitive while preg_split uses case sensitive regex. For case sensitive regex we used to use spliti.
(Everything above from http://phppot.com/php/reasons-why-split-is-deprecated/)
So, the main difference between PCRE and POSIX regex as per php.net (http://php.net/manual/en/function.preg-split.php)
As of PHP 5.3.0, the POSIX Regex extension is deprecated. There are a number of differences between POSIX regex and PCRE regex. This page lists the most notable ones that are necessary to know when converting to PCRE.
- The PCRE functions require that the pattern is enclosed by delimiters.
- Unlike POSIX, the PCRE extension does not have dedicated functions for case-insensitive matching. Instead, this is supported using the i (PCRE_CASELESS) pattern modifier. Other pattern modifiers are also available for changing the matching strategy.
- The POSIX functions find the longest of the leftmost match, but PCRE stops on the first valid match. If the string doesn't match at all it makes no difference, but if it matches it may have dramatic effects on both the resulting match and the matching speed. To illustrate this difference, consider the following example from "Mastering Regular Expressions" by Jeffrey Friedl. Using the pattern one(self)?(selfsufficient)? on the string oneselfsufficient with PCRE will result in matching oneself, but using POSIX the result will be the full string oneselfsufficient. Both (sub)strings match the original string, but POSIX requires that the longest be the result.
- The POSIX definition of a "character class" differs from that of PCRE. Simple bracket expressions to match a set of explicit characters are supported in the form of PCRE character classes but POSIX collating elements, character classes and character equivalents are not supported. Supplying an expression with a character class that both starts and ends with :, . or = characters to PCRE is interpreted as an attempt to use one of these unsupported features and causes a compilation error.
Upvotes: 0
Reputation: 22313
preg_split()
, which uses a Perl-compatible regular expression syntax, which is faster.alternative to split()
.
Upvotes: 0
Reputation: 655619
split
uses POSIX ERE and preg_split
uses PCRE. See this PHP manual page on the differences of PCRE from POSIX ERE as well as the flavor comparison on Regular-Expressions.info.
Upvotes: 2
Reputation: 4366
Easy, split() :
Warning
This function has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged.
Upvotes: 0