By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
 438,303 Members | 1,245 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,303 IT Pros & Developers. It's quick & easy.

# Double backslashes \\ in strings

 P: n/a 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 then the double \\ gets translated to a single \, which isn't what I want. Now, I can write
Share this Question
7 Replies

 P: n/a 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 then the double \\ gets translated to a single \, which isn't what I want. Now, I can write

 P: n/a Erwin Moller < wrote: JohnF schreef: >I have a function textag($expression){...}whose$expression argument is a string thatcan contain substrings like \alpha with onebackslash or like a&b\\c&d with two backslashes.If I write then the double \\ gets translated to a single \,which isn't what I want. Now, I can write

 P: n/a ..oO(JohnF) >The examples on http://www.php.net/manual/en/language.types.string.phpinclude 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'smentioned 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

 P: n/a Michael Fesser >The examples on http://www.php.net/manual/en/language.types.string.phpinclude 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'smentioned 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: 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

 P: n/a JohnF schreef: Erwin Moller < wrote: >JohnF schreef: >>I have a function textag($expression){...}whose$expression argument is a string thatcan contain substrings like \alpha with onebackslash or like a&b\\c&d with two backslashes.If I write then the double \\ gets translated to a single \,which isn't what I want. Now, I can write'; echo 'SQ, BS will escape since the next char is a quote: \'bla'; echo '
'; echo 'SQ, BS will escape since the next char is a BS : \\bla'; echo '
'; echo 'SQ, Now ending a string with SQ: \'bla\''; echo '
'; 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/ ============================ Sep 25 '08 #6

 P: n/a Erwin Moller < wrote: JohnF schreef: >Erwin Moller < wrote: >>JohnF schreef:I have a function textag($expression){...}whose$expression argument is a string thatcan contain substrings like \alpha with onebackslash or like a&b\\c&d with two backslashes.If I write then the double \\ gets translated to a single \,which isn't what I want. Now, I can write

 P: n/a JohnF schreef: Erwin Moller < wrote: >JohnF schreef: >>Erwin Moller < wrote:JohnF schreef:I have a function textag($expression){...}whose$expression argument is a string thatcan contain substrings like \alpha with onebackslash or like a&b\\c&d with two backslashes.>If I write then the double \\ gets translated to a single \,which isn't what I want. Now, I can writeIs there some way to fix this that's transparentto the user calling textag()? I can't reallydo any kind of preg_replace, because that wouldalso change the originally correct \alpha toincorrect \\alpha. Thanks for any suggestions,Nobody mentioned preg_replace. ;-)Read up here:http://www.php.net/manual/en/language.types.string.phpThanks, Erwin. I only mentioned preg_replace becauseit had crossed my mind, but then I realized that wouldbe 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

### This discussion thread is closed

Replies have been disabled for this discussion.