Categorized | News

Using PCRE and the preg_*() Family of Functions

Posted on 23 November 2002 by Demian Turner

from the PHP UK list, Keith Young
When using PCRE (Perl Compatible Regular Expressions) you need to surround the expression to be evaluated with a delimiter.


The above are all equivalent… Most people use // as the delimiters.

There is one more “problem” in the PCRE you wrote.  The “.” in a PCRE denotes ANY character.  So if you had “wwwx” it would match.  To force the “.” to evaulate as a period you need to escape it:


More information about PCRE and how to use it (in PHP) is available on the PHP website (I find the pattern syntax page to be the one I reference more often – link below):

One last point.  If you are doing a straight replace that doesn’t require any level of complexity to the pattern matching, then as someone else suggested, using str_replace is a better solution, since it is much easier on the PHP engine.  This is more noticable in situations where the replacement will occur multiple times (like in
loops and such).

Bookmark and Share

Leave a Reply



Demian Turner's currently-reading book recommendations, reviews, favorite quotes, book clubs, book trivia, book lists



PHPkitchen recommends you also check out the following sites :

Accounting for Small Businesses

FreeAgent sign-up