note 68594 deleted from ref.pcre by colder
| From: | colder@php.net | Date: | Tue, 21 Nov 2006 10:05:42 +0000 |
| Subject: | note 68594 deleted from ref.pcre by colder | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-119929@lists.php.net to get a copy of this message | ||
Note Submitter: filippo dot toso at humanprofile dot biz
----
Here it is a simple PHP function that escapes a string to safely use it in a Regular Expression,
very useful to include unknown strings in PCRE patterns:
<?php
function safe_preg($content) {
$search = array('\\\\', '^', '$', '.', '[',
']', '|', '(', ')', '?', '*',
'+', '{', '}');
$replace = array('\\\\\\\\', '\\^', '\\$', '\\.',
'\\[', '\\]', '\\|', '\\(', '\\)',
'\\?', '\\*', '\\+', '\\{', '\\}');
return str_replace($search, $replace, $content);
}
?>