RE: [PHP] How do I create my own addslashes() style routine for N T directories?

From: Date: Fri, 11 Aug 2000 18:38:38 +0000
Subject: RE: [PHP] How do I create my own addslashes() style routine for N T directories?
Groups: php.general 
Request: Send a blank email to php-general+get-11370@lists.php.net to get a copy of this message
WARNING: Long, detailed explanation of string quoting and regex parsing below. It's probably more that you ever wanted to know. For the solution to the problem, skip almost to the end of the message (above the ----). The problem is not with addslashes(). It does its job well, and exactly as it's supposed to. Instead, the "problem" is with PHP's strings. Check out the man page at http://www.php.net/manual/language.types.string.php. Basically, inside double-quoted strings, \ acts as an escape operator (so you can do stuff like \n and \t). This means that a "\" can't represent \, so if you want to represent \, you have to escape it (using \), and the result is that "\\" represents \. (Confused yet? :) If you use single-quoted strings, \ is still an escape character (so you can do something like 'isn\'t a complete sentence'. The only reason \ is an escape character in single-quoted strings is so that you can include single quotes inside single-quoted strings. However, because it is an escape character, you have to escape \ using \, or in other words, '\\' == \. Despite this, '\' will show up as \ in a single-quoted string *if it is not followed by either a single quote (') or a backslash (\)*. What all this means is that '\My name is \Bruce' evaluates to \My name is \Bruce, as does '\\My name is \Bruce'. This is a long answer, but the basic idea is that these two lines: $script .= "set basedir=\\NTSERVER\some\directory\here\ \n"; $script .= "set anotherdir=\\NTSERVER\some\directory\here\ \n"; Actually make $script equal to this: set basedir=\NTSERVER\some\directory\here\ set anotherdir=\NTSERVER\some\directory\here\ If you had done this instead: $script .= "set basedir=\\NTSERVER\some\new directory\here\ \n"; $script .= "set anotherdir=\\NTSERVER\some\tall directory\here\ \n"; It would be even worse, because \n and \t would be expanded to newline and tab, so you'd end up with: set basedir=\NTSERVER ew some\directory\here\ set anotherdir=\NTSERVER all some\directory\here\ You can fix most of this using single-quoted strings (because they only escape \ and '), like so: $script .= 'set basedir=\\NTSERVER\some\new directory\here\ '."\n"; $script .= 'set anotherdir=\\NTSERVER\some\tall directory\here\ '."\n"; (note the \n is still double-quoted, so that it will be a newline instead of a literal \n. The only problem is that '\\' evaluates to \, and so you end up with: set basedir=\NTSERVER\some\new directory\here\ set anotherdir=\NTSERVER\some\tall directory\here\ The \n and \t are as you would expect, but you still don't get the \\ at the beginning of the string. The only way to fix that is to use $script .= 'set basedir=\\\\NTSERVER\some\new directory\here\ '."\n"; $script .= 'set anotherdir=\\\\NTSERVER\some\tall directory\here\ '."\n"; This gives the correct set basedir=\\NTSERVER\some\new directory\here\ set anotherdir=\\NTSERVER\some\tall directory\here\ So... you'll either have to use single-quoted strings and replace \ with \\ any time it is followed by a \ or a ', or you'll have to read your values in from a file or other external source (which aren't subject to escapes like quoted strings). However, I'd recommend you do this: $script .= "set basedir=//NTSERVER/some/new directory/here/ \n"; $script .= "set anotherdir=//NTSERVER/some/tall directory/here/ \n"; $script = str_replace("/", "\\", $script); This reduces all of your \ escaping headaches to one place (the "\\" in the str_replace). ----- The ereg_replace problem happens for a related reason. The string gets processed (escaped) by PHP before ereg_replace ever sees it. The \ character has special meaning to ereg_replace (escaping special regex characters such as ., *, and +). So, when you do this: $eregscript = ereg_replace("\\","\\\\",$script); PHP first processes it into: $eregscript = ereg_replace(\,\\,$script); It then calls the ereg_replace function, which complains, because it expects the \ character it sees as its first argument to escape something (. or * or something), but it doesn't find anything to escape. It's not as smart as PHP's string parser, which just treats a \ like a \ if it's not followed by a character that needs to be escaped. --Bruce Bruce Christensen Intel Corporation brucex.r.christensen@intel.com -----Original Message----- From: Daevid Vincent [mailto:DayWalker@TheMatrix.com] Sent: Thursday, August 10, 2000 8:10 PM To: PHP General Subject: [PHP] How do I create my own addslashes() style routine for NT directories? We have a database that is supposed to store typical NT fashion paths: \\NTSERVER\some\directory\here\ But I can't figure out how in the heck to get it to save and retrieve in tact. If you exectute the code below, you will see the results at the bottom of the page. (I can't get the ereg_replace() function to work). So, it seems that addslashes() is a bit 'too smart' for it's own good. instead of blindly just converting my $script to "set basedir=\\\\NTSERVER\\some\\directory\\here\\ \n" it assumes that I meant to escape the \\ as is written and skips it! grrrr. thereby creating "set basedir=\\NTSERVER\\some\\directory\\here\\ \n" which is close but no cigar. Anyone have a solution around this annoyance? The situation is compounded by the fact that there are in deed characters that I want to be just what they are, such as the \n character. In this case, the add/stripslashes() routines work perfectly. It's only the case of when it sees \\ that it becomes a problem. ------------------ snip backslashes.phtml ------------------ <html><head><title>back slashes</title></head> <body> <?php $script .= "set basedir=\\NTSERVER\some\directory\here\ \n"; $script .= "set anotherdir=\\NTSERVER\some\directory\here\ \n"; ?> <TEXTAREA COLS="80" ROWS="30" WRAP="off"> <?php echo $script; echo "\n\n"; echo "ADDSLASHES -----------------------------------------\n"; $slashedscript = addslashes($script); echo $slashedscript; echo "\n\n"; echo "STRIPSLASHES ---------------------------------------\n"; $unslashedscript = stripslashes($slashedscript); echo $unslashedscript; echo "\n\n"; echo "QUOTEMETA ------------------------------------------\n"; $quotescript = quotemeta($slashedscript); echo $quotescript; echo "\n\n"; echo "EREG_REPLACE ---------------------------------------\n"; $eregscript = ereg_replace("\\","\\\\",$script); echo $eregscript; ?> </TEXTAREA> </body></html> ------------------ snip output ------------------ set basedir=\NTSERVER\some\directory\here\ set anotherdir=\NTSERVER\some\directory\here\ ADDSLASHES ----------------------------------------- set basedir=\\NTSERVER\\some\\directory\\here\\ set anotherdir=\\NTSERVER\\some\\directory\\here\\ STRIPSLASHES --------------------------------------- set basedir=\NTSERVER\some\directory\here\ set anotherdir=\NTSERVER\some\directory\here\ QUOTEMETA ------------------------------------------ set basedir=\\\\NTSERVER\\\\some\\\\directory\\\\here\\\\ set anotherdir=\\\\NTSERVER\\\\some\\\\directory\\\\here\\\\ EREG_REPLACE --------------------------------------- <br> <b>Warning</b>: REG_EESCAPE in <b>/www/html/backslashes.phtml</b> on line <b>28</b><br> -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net For additional commands, e-mail: php-general-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net

« previous php.general (#11370) next »