05.05.2013 Views

Programming PHP

Programming PHP

Programming PHP

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Quoting for regular expressions<br />

The preg_quote( ) function creates a regular expression that matches only a given<br />

string:<br />

$re = preg_quote(string [, delimiter ]);<br />

Every character in string that has special meaning inside a regular expression (e.g., *<br />

or $) is prefaced with a backslash:<br />

echo preg_quote('$5.00 (five bucks)');<br />

\$5\.00 \(five bucks\)<br />

The optional second argument is an extra character to be quoted. Usually, you pass<br />

your regular expression delimiter here:<br />

$to_find = '/usr/local/etc/rsync.conf';<br />

$re = preg_quote($filename, '/');<br />

if (preg_match("/$re", $filename)) {<br />

// found it!<br />

}<br />

Differences from Perl Regular Expressions<br />

Although very similar, <strong>PHP</strong>’s implementation of Perl-style regular expressions has a<br />

few minor differences from actual Perl regular expressions:<br />

• The null character (ASCII 0) is not allowed as a literal character within a pattern<br />

string. You can reference it in other ways, however (\000, \x00, etc.).<br />

• The \E, \G, \L, \l, \Q, \u, and \U options are not supported.<br />

• The (?{ some perl code }) construct is not supported.<br />

• The /D, /G, /U, /u, /A, and /X modifiers are supported.<br />

• The vertical tab \v counts as a whitespace character.<br />

• Lookahead and lookbehind assertions cannot be repeated using *, +, or ?.<br />

• Parenthesized submatches within negative assertions are not remembered.<br />

• Alternation branches within a lookbehind assertion can be of different lengths.<br />

Perl-Compatible Regular Expressions | 115<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2002 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!