PHP  
downloads | documentation | faq | getting help | mailing lists | | php.net sites | links 
search for in the  
previousrecodeoptions de recherchenext
Last updated: Tue, 09 Jul 2002
view the printer friendly version or the printer friendly version with notes or change language to English | Brazilian Portuguese | Chinese | Czech | Dutch | Finnish | German | Hungarian | Italian | Japanese | Korean | Polish | Romanian | Russian | Spanish | Swedish | Turkish

LXXXVII. Expressions r�guli�res compatibles Perl

La syntaxe des masques utilis�s dans ces fonctions ressemble fort � celle de Perl. Les expressions seront entour�es de d�limiteurs, slash (/), par exemple. N'importe quel caract�re peut servir de d�limiteur, tant qu'il n'est pas alpha-num�rique ou n'est pas un antislash (\). Si un d�limiteur doit �tre utilis� dans l'expression, il faudra l'�chapper avec un antislash. Depuis PHP 4.0.4, vous pouvez utiliser les d�limiteurs (), {}, [], et <>, comme en Perl.

Le d�limiteur final peut �tre suivi d'options qui affecteront la recherche. Voir aussi options de recherche.

Exemple 1. Exemples de masques valides

  • /<\/\w+>/

  • |(\d{3})-\d+|Sm

  • /^(?i)php[34]/

  • {^\s+(\s+)?$}

Exemple 2. Exemples de masques invalides

  • /href='(.*)' - d�limiteur final manquant

  • /\w+\s*\w+/J - option 'J' inconnue

  • 1-\d3-\d3-\d4| - d�limiteur initial manquant

Note�: Les expressions r�guli�res Perl sont disponibles depuis la PHP 4 et PHP 3.0.9.

Le support des expressions r�guli�res est assur� par la librairie PCRE, qui est open source, et �crite par Philip Hazel. Elle est soumise au copyright de l'University of Cambridge, Angleterre. Elle est disponible � .

Table des mati�res
options de recherche --  Options disponibles pour les expressions r�guli�res.
syntaxe des masques --  Fonctionnement des expressions r�guli�res.
preg_grep -- Retourne un tableau avec les r�sultat de la recherche.
preg_match_all -- Expression r�guli�re globale.
preg_match -- Expression r�guli�re standard.
preg_quote --  Echappement des caract�res sp�ciaux des expressions r�guli�res.
preg_replace_callback -- Rechercher/remplacer avec fonction de callback
preg_replace --  Rechercher et remplacer par expression r�guli�re standard.
preg_split --  Eclatement d'une cha�ne par expression r�guli�re.
User Contributed Notes
Expressions r�guli�res compatibles Perl
add a note about notes
[email protected]
14-Jul-2000 04:59

... so I find the solution for PCRE under Linux. Download the source code. Compile it with --with-pcre-regexmake parameter. It's working just fine with PHP 4.0.1pl2. One more time I learned: the safest (and the fastest) way is build your own code from the source. :-)
[email protected]
24-Jul-2000 09:13

Please add a link from the ordinary regular expressions to these. Otherwise they are easy to miss, when looking for regex functions, due the abreviation.
[email protected]
12-Mar-2001 10:09

Ever wondered how to use a function within a PCRE? here's how:

$content = preg_replace("/\*(\w+)\*/e", "lookupImageCode($1)", $content);

This code replaces all instances of *text*, where text is a code for an image, with the output of function lookupImageCode(). The /e and the quotes around the function name and arguments are essential to make this work :-)

I use this on one of my websites to query a database and cross-reference image filenames with special codes input by press release writers in our client's company. They type the code of an image (displayed in a catalogue onscreen on the site's backend) and this little wonder of code changes it to an img src tag on the fly as the page is loaded.

Enjoy!

p.s. thanks to the guys on #php who helped sort this out at 4am in the morning my time! (donut, jfk, cardinal) Contact me if you like.

[email protected]
22-Aug-2001 12:09

There is some really good/simple documentation of Perl regular expressions at
[email protected]
06-Mar-2002 05:33

If you're venturing into new regular expression territory with a lack of useful examples then it would pay to get familiar with this page:


[email protected]
12-Mar-2002 06:56

Here's a translation of Friedl's email address matching regexp:



Friedl's perl original is available here:


[email protected]
14-Jun-2002 03:47

This is the easiest tutorial for regular expressions I have found yet.
Beginners should start here.
This tutorial is originally written for Jscript, but I think everything applies to PHP.
(I had to chop the following url into small pieces to be able to post it here)



script56/html/js56reconRegularExpressions.asp

[email protected]
31-Jul-2002 05:18

Back References should be escaped: such as \\1 \\2

Example:
preg_match("/<(\w+)(.*?)>(.+)<\/\\1>/s", $data, $regs);

This attempts to parse XML/HTML like data... Where:
$regs[1] is the tag name
$regs[2] are any tag properties
$regs[3] is the data within the tag

This handles the base case for an algorithm that could parse an entire XML form recursively.

add a note about notes
previousrecodeoptions de recherchenext
Last updated: Tue, 09 Jul 2002
show source | credits | stats | mirror sites
Copyright © 2001, 2002 The PHP Group
All rights reserved.
This mirror generously provided by:
Last updated: Sat Aug 31 06:19:44 2002 CEST