473,770 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Double backslashes \\ in strings

I have a function textag($express ion){...}
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,
--
John Forkosh ( mailto: j@f.com where j=john and f=forkosh )
Sep 24 '08 #1
7 13047

JohnF schreef:
I have a function textag($express ion){...}
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

--
=============== =============
Erwin Moller
Now dropping all postings from googlegroups.
Why? http://improve-usenet.org/
=============== =============
Sep 24 '08 #2
Erwin Moller < wrote:
JohnF schreef:
>I have a function textag($express ion){...}
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.

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 \.

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,
--
John Forkosh ( mailto: j@f.com where j=john and f=forkosh )
Sep 24 '08 #3
..oO(JohnF)
>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 \.

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.
How will users use your function? Will they call it directly from within
their own scripts or does it receive data from an HTML form? It makes a
difference if you get a string value from some source (form, database,
whatever) or if you create it on your own. In the latter case you have
to take PHP's rules for string parsing and escaping into account, while
in the first case there shouldn't be any problem.

Micha
Sep 24 '08 #4
Michael Fesser <ne*****@gmx.de wrote:
.oO(JohnF)
>>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 \.

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.

How will users use your function? Will they call it directly from within
their own scripts or does it receive data from an HTML form? It makes a
difference if you get a string value from some source (form, database,
whatever) or if you create it on your own. In the latter case you have
to take PHP's rules for string parsing and escaping into account, while
in the first case there shouldn't be any problem.
Thanks, Micha. Specifically, the function will be used as
described at
http://www.forkosh.com/mimetex.html?valignment
That is, users will typically use it from within their own
scripts in exactly the form illustrated in the original post:
<?php textag('\begin{ matrix}a&b\\c&d \end{matrix}); ?>
is a full example with both \'s and \\'s. The markup
syntax is LaTeX's, and it would be nice if users could
write it in its standard form. Writing \\\\ for \\
might be hard (and inconvenient) for them to remember all the time.
You seem to be saying that "in the first case there
shouldn't be any problem". But isn't this your first case?
And I'm not seeing how to avoid the problem. Thanks again,
--
John Forkosh ( mailto: j@f.com where j=john and f=forkosh )
Sep 24 '08 #5

JohnF schreef:
Erwin Moller < wrote:
>JohnF schreef:
>>I have a function textag($express ion){...}
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.
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.
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_gp c.

Regards,
Erwin Moller

--
=============== =============
Erwin Moller
Now dropping all postings from googlegroups.
Why? http://improve-usenet.org/
=============== =============
Sep 25 '08 #6
Erwin Moller < wrote:
JohnF schreef:
>Erwin Moller < wrote:
>>JohnF schreef:
I have a function textag($express ion){...}
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
interprete d 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.
--
John Forkosh ( mailto: j@f.com where j=john and f=forkosh )
Sep 26 '08 #7

JohnF schreef:
Erwin Moller < wrote:
>JohnF schreef:
>>Erwin Moller < wrote:
JohnF schreef:
I have a function textag($express ion){...}
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
expressio n argument in single quotes, then that
works fine, and the single backslash isn't
interpret ed 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/
=============== =============
Oct 2 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
8262
by: sinister | last post by:
The examples in the online manual all seem to use double quotes, e.g. at http://us3.php.net/preg_replace Why? (The behavior is different with single quotes, and presumably simpler to understand.)
7
4997
by: Margaret MacDonald | last post by:
I've been going mad trying to figure out how to do this--it should be easy! Allow the user to enter '\_sometext\_', i.e., literal backslash, underscore, some text, literal backslash, underscore and, after submitting via POST to a preg_replace filter, get back '_sometext_' (i.e., the same thing with the literal backslashes stripped)
6
24581
by: Mikheil | last post by:
Hello! I need to translate file destination name with one backslashes "c:\program files\directory\file.txt" to string containing double backslashes "c:\\program files\\directory\\file.txt" If there is a nativi function on C++, or any algolithms I'd be glad to know about them. Thank you
6
2049
by: supercomputer | last post by:
I am using this function to parse data I have stored in an array. This is what the array looks like: , , , , , , , , , , , , , , , , , , , , , , , ] This is the code to parse the array:
2
3532
by: wylbur37 | last post by:
When using a form with an input textbox such as the following ... <input type="text" name="field1" size=30> I discovered that when a backslash (\) is typed into the textbox, when I later check the value (in $field1), I get *two* backslashes. For example, If I type ... c:\abc\xyz
3
23013
by: Stef Mientki | last post by:
It looks like sometimes a single backslash is replaced by a double backslash, but sometimes it's not ??? See the error message below, the first backslash is somewhere (not explicitly in my code) replaced, but the second is not ??? Is it in general better to use double backslash in filepaths ? thanks, Stef Mientki
3
13492
by: =?Utf-8?B?cmFuZHkxMjAw?= | last post by:
OpenFileDialog gives me the following, which I place in tbDevPath.Text: x:\\myVob\\mySolution\\mySolution.sln I really need this path to be single-backslashes, not double-backslashes, so I tried the following: string updatedDevPath = Regex.Replace(tbDevPath.Text, @"^\\\\$", "\\") This still leaves me with double-backslashes. Any suggestions?
23
13523
by: dkirkdrei | last post by:
I am having a bit of trouble trying to double up on slashes in a file path. What I am trying to do is very similar to the code below: <? $var = "\\wusais\Intranets\Intranets\fpdb\pdf\weinig\00505882.pdf"; $new = preg_replace("\\", "\\\", "$var"); ?> Code above produces the following error:
6
3189
by: Joseph Stateson | last post by:
I just started calling a php module from html. I added "php rocket" from microsoft to FP2003 but dont think that is the cause. The problem is that I am getting a backslash before a double or single quote and I cannot figure out how to get ride of it. $query = 'SELECT * FROM cpuinfo where Description like "%xcell%" or Description like "%q6600%" '; The above works perfectly but if I attempt to pass the sql string into the
0
9619
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10102
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9910
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6712
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.