..


Sponsored Links

PCRE regular expression functions (and differences with the POSIX functions)

Article written by Max Bossi
Page 1 of 4

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.

Comparisons: the preg_match ()

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 / s

 
Let's see the syntax:
  • / ... / - Delimit the beginning and the end of the expression;
  • mrwebmaster - is a simple string which you want to check the text passed as the second argument;
  • i - "i" after the closing delimiter indicates that the search should be NOT case sensitive (just as was done by using the function eregi ()). Technically this "i" is a modifier, in this page a complete list of modifiers allowed in the syntax of PCRE.

The difference with ereg ()

As you can see the difference with ereg () are not a lot, let's summarize them briefly:

  • use of delimiters / ... / (actually you can use other characters as a delimiter, but the slash is the usual choice);
  • case-insensitive version (eregi ()) is replaced by the use of "i" after the closing delimiter;
  • the presence of the character / in the pattern will require (if you are used to slash as delimiter) escape (\ /) in order not to be confused with the delimiters;
  • There are some minor differences in the management of ASCII characters (the POSIX functions, unlike the PCRE accept the numeric ASCII characters: eg. corresponds to the number 10 and 13 new line carriage return; vice versa with the PCRE functions 10 represents exclusively the number ten).
  • the function ereg () accepts only three parameters (pattern, text to be checked and, optionally, an array to hold the results) and preg_match () admits five (the first two, we have already seen, are required, the other three are optional );

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 .

In the same category ...
E-Learning
Linux (Course) Linux (Course)
Complete guide to open-source system. From 49 €.
MySQL (Course) MySQL (Course)
Management of open-source database. From 39 €.
PHP (Course) PHP (Course)
Full course for creating dynamic Web sites. From 49 €.
Sponsored Links