RE: [PHP] Backslashing the [ and ] in a regex
| From: | Adam Bregenzer | Date: | Wed, 04 Feb 2004 22:19:38 +0000 |
| Subject: | RE: [PHP] Backslashing the [ and ] in a regex | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-176846@lists.php.net to get a copy of this message | ||
On Wed, 2004-02-04 at 16:35, Sam Masiello wrote:
> Thank you for the reply, Adam, but unfortunately it didn't work.
Sorry bout that. Here's another shot at it.
If I understand your goal correctly you want to escape the existing
backslashes that proceed certain special characters (namely
[:alpha:]+*/.|<>()$). That works fine so far, but you want to add the
[ and ] characters to the list I am guessing. Here is one way you could
do it:
$rule = ereg_replace("\\\\[[:alpha:]+*/.|<>()$[\[]", "\\\\0", $rule);
That turns this:
a\a\+\-\*\[\{\<\|
into this:
a\\a\\+\-\\*\\[\{\\<\\|
However, you will have a problem if there are double escaped characters,
or characters that are escaped but on in your list. I suggest doing
this:
$rule = ereg_replace("\\\\", "\\\\", $rule);
This turns this:
a\a\+\-\*\\a+\\-\\*\\a\\[\[\{\\{\<\\<\|\\|
into this:
a\\a\\+\\-\\*\\\\a+\\\\-\\\\*\\\\a\\\\[\\[\\{\\\\{\\<\\\\<\\|\\\\|
which should back out nicely when handled by postgres.
--
Adam Bregenzer
adam@bregenzer.net
http://adam.bregenzer.net/