PHP 8.4.0 RC2 available for testing

Fonctions PCRE

Sommaire

  • preg_filter — Recherche et remplace avec une expression rationnelle
  • preg_grep — Retourne un tableau avec les résultats de la recherche
  • preg_last_error — Retourne le code erreur de la dernière expression PCRE exécutée
  • preg_last_error_msg — Renvoie le message d'erreur de la dernière exécution de regex PCRE
  • preg_match — Effectue une recherche de correspondance avec une expression rationnelle standard
  • preg_match_all — Expression rationnelle globale
  • preg_quote — Protection des caractères spéciaux des expressions régulières
  • preg_replace — Rechercher et remplacer par expression rationnelle standard
  • preg_replace_callback — Rechercher et remplacer par expression rationnelle standard en utilisant une fonction de callback
  • preg_replace_callback_array — Éffectue une recherche de correspondance avec une expression régulière et remplace grâce à une fonction de rappel
  • preg_split — Éclate une chaîne par expression rationnelle
add a note

User Contributed Notes 3 notes

up
6
steve at stevedix dot de
20 years ago
Something to bear in mind is that regex is actually a declarative programming language like prolog : your regex is a set of rules which the regex interpreter tries to match against a string. During this matching, the interpreter will assume certain things, and continue assuming them until it comes up against a failure to match, which then causes it to backtrack. Regex assumes "greedy matching" unless explicitly told not to, which can cause a lot of backtracking. A general rule of thumb is that the more backtracking, the slower the matching process.

It is therefore vital, if you are trying to optimise your program to run quickly (and if you can't do without regex), to optimise your regexes to match quickly.

I recommend the use of a tool such as "The Regex Coach" to debug your regex strings.

http://weitz.de/files/regex-coach.exe (Windows installer) http://weitz.de/files/regex-coach.tgz (Linux tar archive)
up
3
theking2 at king dot ma
11 months ago
Writing regexp can be tedious and error prone. Test yours on https://regex101.com/ to make sure you haven't missed anything . It even colour codes example strings.
up
4
stronk7 at moodle dot org
17 years ago
One comment about 5.2.x and the pcre.backtrack_limit:

Note that this setting wasn't present under previous PHP releases and the behaviour (or limit) under those releases was, in practise, higher so all these PCRE functions were able to "capture" longer strings.

With the arrival of the setting, defaulting to 100000 (less than 100K), you won't be able to match/capture strings over that size using, for example "ungreedy" modifiers.

So, in a lot of situations, you'll need to raise that (very small IMO) limit.

The worst part is that PHP simply won't match/capture those strings over pcre.backtrack_limit and will it be 100% silent about that (I think that throwing some NOTICE/WARNING if raised could help a lot to developers).

There is a lot of people suffering this changed behaviour from I've read on forums, bugs and so on).

Hope this note helps, ciao :-)
To Top