add a note

User Contributed Notes 8 notes

Hayley Watson
6 years ago
As a rule of thumb, it's better to describe your regular expression patterns using single-quoted strings.

Using double-quoted strings, the interaction between PHP's and PCRE's interpretations of which bits of the string are escape sequences can get messy. Regular expressions can get messy enough as it is without another layer of escaping making it worse.
sam marshall
17 years ago
For anyone who sees this error:

Warning: preg_match() [function.preg-match]: Compilation failed: PCRE does not support \L, \l, \N, \P, \p, \U, \u, or \X at ...

As this manual page says, you need PHP 5.1.0 and the /u modifier in order to enable these features, but that isn't the only requirement! It is possible to install later versions of PHP (we have 5.1.4) while linking to an older PCRE install. A quick look at the PCRE changelog suggests that you probably need at least PCRE 5; we're running 4.5, while the latest is 7.1. You can find out your PCRE version by checking phpinfo().

I suspect this ancient PCRE version is included in some officially-supported Red Hat Enterprise package which is probably why we are running it so might also affect other people.
J Daugherty
19 years ago
In the character class meta-character documentation above, the circumflex (^) is described:

"^ negate the class, but only if the first character"

It should be a little more verbose to fully express the meaning of ^:

^ Negate the character class. If used, this must be the first character of the class (e.g. "[^012]").
napalm at spiderfish dot net
20 years ago
Pay attention that some pcre features such as once-only or recursive patterns are not implemented in php versions prior to 5.00

pstradomski at gmail dot com
17 years ago
About strip_selected_tags function from two posts below:

it does not work if somebody uses tags without ending ">" character, like this:

<p <b> bold text </b</p

This is even valid HTML (but not valid XHTML)
onerob at gmail dot com
19 years ago
If, like me, you tend to use the /U pattern modifier, then you will need to remember that using ? or * to to test for optional characters will match zero characters if it means that the rest of the pattern can continue matching, even if the optional characters exist.

For instance, if we have this string:


and apply this pattern:


The whole pattern is matched but none of the _ characters are placed in the sub-pattern. The way around this (if you still wish to use /U) is to use the ? greediness inverter. eg,

info at atjeff dot co dot nz
19 years ago
ive never used regex expressions till now and had loads of difficulty trying to convert a [url]link here[/url] into an href for use with posting messages on a forum, heres what i manage to come up with:

$patterns = array(
$replacements = array(
"<a href=\"\\1\">\\1</a>",
"<a href=\"\\1\">\\1</a>",
"<img src=\"\\1\">",

$newText = preg_replace($patterns,$replacements, $text);

at first it would collect ALL the tags into one link/bold/whatever, until i added the "?" i still dont fully understand it... but it works :)
Daniel Vandersluis
18 years ago
Concerning note #6 in "Differences From Perl", the \G token *is* supported as the last match position anchor. This has been confirmed to work at least in preg_replace(), though I'd assume it'd work in preg_match_all(), and other functions that can make more than one match, as well.
To Top