Re: quotemeta() question...

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Jason Barnett wrote:
> Richard Lynch wrote:
>> Steve Kaufman wrote:
>>
>>>Why does
>>>          quotemeta("pat:1$WRW")
>>>return
>>>          pat:1
>>>instead of
>>>          pat:1\$WRW
>>>
>>>What am I misunderstanding about quotemeta function?
>>
>>
>> You usually would use quotemeta on data coming from the database, or the
>> user, or externally, or, errr, basically things you haven't typed in to
>> PHP, that you need to pass into Regular Expressions.
>>
>> In those cases, you've already got the $ (and other characters)
>> successfully embedded in the string, but you want to escape them for
>> whatever reason.
>>
>> A better example code would be:
>> $string =
>> 'period.backslash\\plus+star*question?lbracket[rbracket]carat^lparen(rparen)dollar$';
>> echo "<PRE>", quotemeta($string), "</PRE>";
>>
>
> Interesting aside... with the test string above, I noticed that
> backslash\\ only resolved to two backslashes.  I thought there would be
> 4.  It seems that quotemeta will resolve \ and \\ to \\, \\\ and \\\\
> resolve to \\\\, etc.

No, no, no.

FORGET quotemeta for a moment.

*BEFORE* the string ever GETS to quotemeta, PHP takes the characters you
typed and builds a string.

For PHP, \\ inside of apostrophes turns into a single \.

You Type:          Internal PHP string
'\\'               \
'\''               '
'\\\\'             \\
"\\"               \
"\""               "

It's absolutely crucial that you understand that PHP itself alters the \\
to \ lonnnnnnng before quotemeta enters the picture.

If you use just '\' in what you type, PHP is kinda stuck with just
assuming you meant '\\' and it runs with \ as its internal representation.

This is documented behaviour, but I consider it Bad Style.

So using \ or \\\ or any odd number of \ inside of apostrophes/quotes to
get a backslash in the internal PHP string is just plain bogus, if you ask
me. [shrug]

At any rate, quotemeta is *NOT* the thing that converts \ and \\ into \\.

PHP converts \ and \\ into a single \
quotemeta converts that single \ into \\

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux