On 14 September 2004 10:19, Christophe Chisogne wrote: > 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.
[....] > 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?) It's really very simple: (1) PHP processes the string, doing all PHP escape substitutions, and hands the result off to the pcre library; (2) pcre applies its escapes to the received string and then performs the match. > 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? Not quite, since \n is also a valid escape sequence in PHP when used in double-quoted strings; so "\\\n" will be escape-processed by PHP to give the two-character sequence backslash-newline, whereas "\\\\n" will yield the 3-character sequence backslash-backslash-n. Of course, this is probably a moot point since pcre will then interpret the backslash-n sequence in the latter into a newline, but it's as well to be aware of it anyway. > 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. Well, the way I usually do it is to use the reverse order of the two rules above, so: - construct your pcre regex, including any necessary pcre escapes - then run through it inserting PHP escapes; this is a lot easier if you use single-quoted PHP strings, since the only valid PHP escape sequence in that case is \', so only backslashes preceding a ' or a \ actually require doubling. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php