Re: Double backslashes \\ in strings




JohnF schreef:
Erwin Moller < > wrote:
JohnF schreef:
I have a function textag($expression){...}
whose $expression argument is a string that
can contain substrings like \alpha with one
backslash or like a&b\\c&d with two backslashes.

If I write <?php textag('\alpha'); ?> with the
expression argument in single quotes, then that
works fine, and the single backslash isn't
interpreted or changed, which is what I want.
But if I write <?php textag('a&b\\c&d'); ?>
then the double \\ gets translated to a single \,
which isn't what I want. Now, I can write
<?php textag('a&b\\\\c&d'); ?> to get a&b\\c&d,
but that's quit inconvenient and kludgey.

Is there some way to fix this that's transparent
to the user calling textag()? I can't really
do any kind of preg_replace, because that would
also change the originally correct \alpha to
incorrect \\alpha. Thanks for any suggestions,
Hi,
Nobody mentioned preg_replace. ;-)
Read up here:
http://www.php.net/manual/en/language.types.string.php
Regards,
Erwin Moller

Thanks, Erwin. I only mentioned preg_replace because
it had crossed my mind, but then I realized that would
be barking up the wrong tree.



Hi,

Well, the problem is still a little vague to me.

And yes, escaping can be very confusing. ;-)
Try feeding \ to complex regular expression from PHP if you want a heavy headache. ;-)

The examples on
http://www.php.net/manual/en/language.types.string.php
include these two...
// Outputs: You deleted C:\*.*?
echo 'You deleted C:\*.*?';
and
// Outputs: You deleted C:\*.*?
echo 'You deleted C:\\*.*?';
which indeed illustrate the problem,
i.e., \ outputs \, but \\ also outputs \.

Correct.
Reason is that with single quote the \ is treated in the context you use it in.
[quote]
Single quoted

The simplest way to specify a string is to enclose it in single quotes (the character ').

To specify a literal single quote, escape it with a backslash (\). To specify a literal backslash before a single quote, or at the end of the string, double it (\\). Note that attempting to escape any other character will print the backslash too.
[/quote]

Try running these, and see if it makes sense to you:
SQ = single quotes, and BS is .... backslash. :P

echo 'SQ, BS will not escape since the next char is a b: \bla';
echo '<br>';
echo 'SQ, BS will escape since the next char is a quote: \'bla';
echo '<br>';
echo 'SQ, BS will escape since the next char is a BS : \\bla';
echo '<br>';
echo 'SQ, Now ending a string with SQ: \'bla\'';
echo '<br>';
echo 'SQ, Now ending a string with SQ with BS in it: \'\\bla\\\'';




But I don't see any way to solve it that's
mentioned there, short of writing
// Outputs: You deleted C:\\*.*?
echo 'You deleted C:\\\\*.*?';
which is what I don't want to force users to do.
That is, if they want \\, they should be able to type \\
rather than \\\\. Am I missing something on that
page that addresses this problem? Thanks again,

You might have a look at the function addslashes()
http://nl3.php.net/manual/en/function.addslashes.php

Tip: Do NOT use addslashes to make a string safe for use in SQL, it is not enough.

Hope this helps.

And, test a lot, also check your php.ini for possible confusing settings like magic_quotes_gpc.

Regards,
Erwin Moller

--
============================
Erwin Moller
Now dropping all postings from googlegroups.
Why? http://improve-usenet.org/
============================
.



Relevant Pages

  • Re: heredoc and array problems
    ... When you echo an array variable such as $Din SINGLE QUOTES ... you need to concatenate it as single quotes means a string literal ... So the implied interpolation works only inside strings. ...
    (comp.lang.php)
  • Re: preserving backslashes
    ... echo $'LINE'>> foo.conf ... everything works except the backslash preserving. ... the config file is not listed like that. ... single quotes because it will just put int $LINE instead of the actual ...
    (comp.unix.shell)
  • Re: Unsolvable? How to extract dollar amount from $1.12 stored in a file
    ... single quotes or backslash to modify the string. ...
    (perl.beginners)
  • Re: FTP to a windows file share?
    ... You'll need to double the first backslashes, because a double backslash - being the escape character - evaluates to a single one even in single quotes. ... a closing backslash needs to be escaped at the end of a single quoted string because it would be interpreted as an escaped single quote otherwise, which results in an error complaining about an unterminated string. ...
    (comp.lang.perl.misc)
  • Re: Escapeism
    ... How do you echo them using print? ... "rawrification" applies only to string literals and not to string ... dumb question - is the backslash as escape character fixed or can one set its ...
    (comp.lang.python)