Re: perl regex in php and multiple escape rules
| From: | Wouter van Vliet | Date: | Tue, 14 Sep 2004 09:29:19 +0000 |
| Subject: | Re: perl regex in php and multiple escape rules | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-196598@lists.php.net to get a copy of this message | ||
On Tue, 14 Sep 2004 11:18:33 +0200, Christophe Chisogne
<christophe@publicityweb.com> wrote:
>
> In a word:
>
> I'm looking for more detailed information about preg_replace
> (and other perl regex functions) than in the php manual,
> specifically about different escape rules interaction.
>
> In more words:
>
> PHP has it's own way of escaping strings [2]
> Ex \ within '' is '\' (or '\\' if at the end or before ' )
> \ within "" is "\" (or "\\" if at the end or before " )
> So \\ can be written '\\\' or '\\\\' or "\\\" or
> "\\\\"
> and \\\ can be written '\\\\\' or '\\\\\\' (same with " )
> (rule 1)
>
> Perl regex are powerfull and came with other escape rules [3]
> Ex regex to match... is ...
> \ /\\/
> (newline) \n /\n/
> (2 chars) \n /\\n/
> (rule 2)
>
> My problem is about preg_replace function, because it's entry in
> the php manual [1] is not specific enough -- I mean, writing
> a real specification seems impossible without more details
>
> The 'pattern' argument is a string, but how does php proceed it?
> I guess it first uses rule1 then rule2, ie php string escape rule
> (for ' " and \ ) then perl regex rule (via verbatim use in perlre C library?)
>
> This mean that to match \n (the 2 chars), the perl re is \\n
> so correct php pattern is '\\\n' or '\\\\n' or "\\\n" or
> "\\\\n".
> (see comment 29-Mar-2004 05:46 on [1]). Is this right?
> /me think using perl regex is easier in perl than in php ;-)
>
> Is it the same for the 'replacement' argument?
>
> Another comment (steven -a-t- acko dot net, 08-Feb-2004 12:45) says
> "To make this easier, the data in a backreference with /e is run through
> addslashes() before being inserted in your replacement expression."
> Is that user right?
>
> Ok, I can try to guess answers to my questions by probing things.
> But that didnt tell me if my guesses are wrong, or if what I guess
> is exactly what php pcre functions are supposed to do
> (not only now with php x.y.z but in the future too).
> And I prefer specifications over guesses.
> (think about ppl using alt attribute instead of title
> on img html tags : they guessed wrong by not reading html spec)
>
> In other words, is there some details about escape rules
> in pcre php functions? I feel much better when I can use
> a stable, reliable and precise API.
>
> Christophe
>
> [1] preg_replace in php manual
> http://www.php.net/manual/en/function.preg-replace.php
>
> [2] strings in php manual
> http://www.php.net/manual/en/language.types.string.php
>
> [3] pcre syntax in php manual
> http://www.php.net/manual/en/reference.pcre.pattern.syntax.php
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
It's all very easy, actually. You take the real regex, and convert
that to a php string.
for example:
to match the two chars \n, in perl you'd do:
/\\n/
php requires each slash to be slashed again, so you'd get
$regex = '/\\\\n/';
whenever you're in doubt, put the regex into a var, print that var and
if that what you get is exactly the regex you'd use in perl, you're
good. And yes, I do agree with anybody who'd state that it's a bit
confusing. Cuz it is!