..
We had already hinted in recent days through the pages of our blog, but today I would like to investigate a topic that I believe will be of interest for thousands of developers - like me - every day use PHP to develop their web applications.
Starting with version 5.3 of PHP, in fact, developers who want to exploit the potential of regular expressions can no longer make use of POSIX functions (so to speak ereg () and family) but must, necessarily, use the functions PCRE (Perl Compatible Regular Expressions), for example, preg_match () .
The PCRE functions built into PHP allow you to use the typical regular expression syntax of Perl (more precisely, the current implementation provides support to the syntax of Perl 5.005), which is slightly different from that of the POSIX functions. For more information on the syntax of PCRE is possible to consult the manual available on the official website PCRE.org.
If you wish to make a comparison between a string and a pattern (a model written in the form of regular expressions) will need to use preg_match () that, in fact, replace ereg () and eregi ().
Using preg_match () is in common use, quite simple: you pass the function the pattern and the string to check. If the pattern is satisfied, the function returns 1 if 0 (or FALSE if an error).
In fact migrate from ereg () is not complicated thing. You will need some little ingenuity in the formulation of the pattern.
Here's an example:
<? Php
if (preg_match ('/ mrwebmaster / i', 'Long live MrWebmaster!')) {
echo "Found";
Else {}
echo "NOT found";
}
?>
This is the same:
<? Php
if (eregi ('mrwebmaster', 'Long live MrWebmaster!')) {
echo "Found";
Else {}
echo "NOT found";
}
?>
In our simple example of using preg_match () we used the string as a pattern
/ Mrwebmaster / sLet's see the syntax:
As you can see the difference with ereg () are not a lot, let's summarize them briefly:
On the basis of the above (and differences that have been mentioned) we can consider again (partially) valid theoretical explanations about the syntax of regular expressions can be found at this page of the article "Regular Expressions in PHP" dedicated to the old POSIX functions .
| |
Linux (Course)
Complete guide to open-source system. From 49 €. |
| |
MySQL (Course)
Management of open-source database. From 39 €. |
| |
PHP (Course)
Full course for creating dynamic Web sites. From 49 €. |