Re: Double backslashes \\ in strings




JohnF schreef:
Erwin Moller < > wrote:
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,
Nobody mentioned preg_replace. ;-)
Read up here:
http://www.php.net/manual/en/language.types.string.php
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.
Well, the problem is still a little vague to me.

An analogous problem would be: suppose you want to echo two backslashes; you'd have to use the statement echo '\\\\'; with four backslashes instead. Now, I can see why you have to type echo "\\\\"; since escaped chars are translated inside "double quotes". But they're not translated inside 'single quotes', so why can't you type echo '\\'; to get \\? Maybe it's still vague why I seem so lazy that
I don't want to type a few extra backslashes. That's
because the real problem is that users will be typing
these strings, not to echo them, but as arguments to
a function like <?php textag('a\\b'); ?> where
the letter a is followed by two \\ backslashes
(not one \) and then followed by the letter b.
Users will not expect to need to type four
backslashes when they want two, and that's liable
to cause problems I'd rather avoid.

Did you read my response?
Did you follow and read the links I sent you?
Did you understand it all?

All your confusion and problems are unneeded, nor is it needed for the users to type 4 slashes if they want 2.

Please reread my last posting and make sure you understand everything.

Regards,
Erwin Moller



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



Relevant Pages

  • Re: Bourne shell sed script help
    ... The script should be self-explanatory. ... echo "Starting text. ... echo "the single quotes replaced with two single quotes" ... echo "single backslashes replaced with double backslashes" ...
    (comp.unix.shell)
  • Re: preserving backslashes
    ... to a new config file. ... Why are you using echo -e here? ... the -E parameter prevents backslash substitution, ... preserve the backslashes in the file. ...
    (comp.unix.shell)
  • Bourne shell sed script help
    ... I need to write a Bourne Shell script to prepare ... echo "Starting text. ... echo "the single quotes replaced with two single quotes" ... echo "single backslashes replaced with double backslashes" ...
    (comp.unix.shell)
  • Re: Bourne shell sed script help
    ... my nearly successful script to format it how it ... echo "Starting text. ... echo "the single quotes replaced with two single quotes" ... echo "single backslashes replaced with double backslashes" ...
    (comp.unix.shell)
  • Re: Backslash interpretation
    ... then `echo' interprets '\\' as a single backslash to print. ... as newline as in your example (and as with double quotes too). ... `echo' is interpreting backslashes passed to it. ...
    (comp.unix.shell)