|
Posted by Richard Lynch on 09/30/21 11:08
Jochem Maas wrote:
> Alex Gemmell wrote:
>> On Mon, 14 Feb 2005 22:51:42 +0100, Jochem Maas <jochem@iamjochem.com>
>> wrote:
>>
>>>Alex Gemmell wrote:
>>>> #Contain at least one number
>>>> if ( !preg_match ('/\\d/', $password) ) return false;
>>>
>>>Im pretty sure the double backslash is a typo.
No, it's *NOT* a typo.
Inside of apostrophes, \ *IS* a special character.
It's also special inside of quotes, but let's stick with apostrophes for now.
\ is used to escape an apostrophe inside of apostrophes.
So you can do:
$var = 'Don\'t forget to escape apostrophes!';
Because \ is used to escape the apostrophe, you also use it to escape the
backslash itself:
$var = 'Backslash \\ should also be escaped';
Inside apostrophes, those are the ONLY two "special" cases:
\' turns into apostrophe: '
\\ turns into backslash: \
Inside quotes, you've got a bunch more like \n, \r, \t as well as variable
substitution going on.
Now, because ONLY those two special cases exist, you *CAN* type:
$var = '\d';
and PHP will store internally: \d
because the backslash wasn't followed by ' or \, so it must be just a
literal backslash.
But that doesn't make it Good Style.
In my *opinion* one should use:
$var = '\\d';
so that it is completely clear that the backslash is being escpaed, and
you're not trying to get a control-D (end of file) or whatever.
Or, put it this way:
It's incredibly unlikely that PHP will ever change '\d' to mean control-D.
But it'e even MORE unlikely that PHP would change '\\d' to mean anything
other than: \d
The same sort of paradigm is true for quote marks -- You *CAN* get away
with a single \ inside of quotes, so long as it's not followed by:
n
r
t
$
"
x[0-9A-Fa-f]{1,2} (a Hex number with \x in front)
[0-7]{1,3} (an Octal number with \ in front)
But with *THAT* many possible following characters, people have gotten
more in the habit of using \\ inside quotes.
But PHP is consistent with *BOTH* apostrophes and quotes about the \
character: There are always a limited number of character combinations
that can follow \ to give it special meaning. Any other character
combination following \ just uses \ literally.
\d always turns into \d inside quotes or apostrophes.
\a always turns into \a
\b turns into \b
..
..
..
ONLY \", \n, \r, \t, etc (see above) turn into something else inside quotes.
ONLY \' and \\ turn into something else inside apostrophes.
To be CLEAR in your code, however, I highly recommend using \\ in ALL
strings when you want a literal backslash -- so that there is no question
whether you are trying to escape the next character as something special,
or you just want a backslash.
\\ *always* means a single backslash
\ sometimes means a single backslash, and sometimes means something
entirely different depending on what follows it.
Use \\, so you *always* know what is going on.
> well you have to escape the bslash if your using double quotes.
> there is a reason sane people (normally) write regexps in php
> using single quotes :-)
Unless your regex contains apostrophes a lot, and then quotes are easier. :-)
//Bad Example: (strstr would be better choice here)
$contraction = ereg("'", $word);
I'm sure others will disagree with me, but this is my opinion on writing
clear code with \ inside of apostrophes and quotes:
Always use \\ to get a literal backslash, and you'll never get confused,
because \\ always means one (1) backslash.
--
Like Music?
http://l-i-e.com/artists.htm
Navigation:
[Reply to this message]
|