472,985 Members | 2,855 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,985 software developers and data experts.

preg functions: use single or double quotes?

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.)
Jul 17 '05 #1
5 8187
When it comes to regular expressions, single quotes don't have a distinct
advantage, in terms of simpler syntax, over double quotes, since you have to
escape backslashes in both type of strings. Meanwhile you can do variable
interpolation in doubly quoted strings. So I think "" has the advantage.

Uzytkownik "sinister" <si******@nospam.invalid> napisal w wiadomosci
news:nk*******************@nwrddc01.gnilink.net...
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.)

Jul 17 '05 #2
sinister wrote:
The examples in the online manual all seem to use double quotes, e.g. at
http://us3.php.net/preg_replace

Why?
Dunno. You might not be too wrong if you were to ascribe that to
inertia. Remember that examples are just examples -- illustrations of
particular methods, if you like -- and aren't intended as best-
practice guidelines. The Manual doesn't urge you to copy their
constructs character for character. It's sometimes necessary to
question authority. ;-)
(The behavior is different with single quotes, and presumably simpler
to understand.)


Indeed the behaviour is different, but I don't believe it's that much
simpler to understand, as long as you know how single- and double-
quoted strings are parsed differently.

http://www.php.net/manual/en/language.types.string.php

As regards to the question in your Subject line (the Subject line is
no substitute for the body of a message; at least yours is
informative though), which was "preg functions: use single or double
quotes?", that's up to you. If, for instance, you need variables to
be parsed in your pattern, then you can't use single-quotes.

There's a nice example given in the documentation for preg_replace on
the 1st Nov. 2003. It concerns how to go about matching the two-
character sequence "\n" (a backslash followed by a lowercase letter
"n"). Consider the string

$foo = '\n'

or,

$foo = "\\n"

The sequence "\n" is special in a regular expression, in that the two
characters stand for a linefeed character. Now, in a single-quoted
pattern, you must precede the "n" by three backslashes:

preg_match('`\\\n`',$foo)

The reason is that a backslash followed by another backslash results
in a single literal backslash in single-quoted strings. The Manual
isn't at all clear on this point IMO, although it does say as much,
in a roundabout kind of way. If we had just two backslashes, the
regular expression would only match a newline character, just as
would happen with a single backslash. (A single and double backslash
behave identically here because a single backslash followed by a
letter is not special in single-quoted strings, but a double
backslash is transformed into a single backslash.)

In a double-quoted pattern however, we need four backslashes before
the "n". This is because in double-quoted strings, a backslash
following a backslash results in a single literal backslash, just as
in single-quoted strings, but additionally, special "escape
sequences" are recognised within double-quoted strings. "\n" means a
linefeed character. To stop "\n" from having that meaning, we need to
escape the backslash with another backslash. Thus, we must use four
backslashes in total:

preg_match("`\\\\n`",$foo)

--
Jock
Jul 17 '05 #3

"Chung Leong" <ch***********@hotmail.com> wrote in message
news:Z6********************@comcast.com...
When it comes to regular expressions, single quotes don't have a distinct
advantage, in terms of simpler syntax, over double quotes, since you have to escape backslashes in both type of strings. Meanwhile you can do variable
interpolation in doubly quoted strings. So I think "" has the advantage.
I agree with what you say. By "simpler" I meant that fewer backslashes are
needed.

Uzytkownik "sinister" <si******@nospam.invalid> napisal w wiadomosci
news:nk*******************@nwrddc01.gnilink.net...
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.)


Jul 17 '05 #4

"John Dunlop" <jo*********@johndunlop.info> wrote in message
news:MP************************@news.freeserve.net ...
sinister wrote:
The examples in the online manual all seem to use double quotes, e.g. at
http://us3.php.net/preg_replace

Why?
Dunno. You might not be too wrong if you were to ascribe that to
inertia. Remember that examples are just examples -- illustrations of
particular methods, if you like -- and aren't intended as best-
practice guidelines. The Manual doesn't urge you to copy their
constructs character for character. It's sometimes necessary to
question authority. ;-)


That's an interesting aside. I understand that reference material cannot
dwell exclusively on best practices, but IMHO they ought to be emphasized.
(The behavior is different with single quotes, and presumably simpler
to understand.)
Indeed the behaviour is different, but I don't believe it's that much
simpler to understand, as long as you know how single- and double-
quoted strings are parsed differently.

http://www.php.net/manual/en/language.types.string.php

As regards to the question in your Subject line (the Subject line is
no substitute for the body of a message; at least yours is
informative though), which was "preg functions: use single or double
quotes?", that's up to you. If, for instance, you need variables to
be parsed in your pattern, then you can't use single-quotes.


Right. In my example, I didn't need variable interpolation.
There's a nice example given in the documentation for preg_replace on
the 1st Nov. 2003. It concerns how to go about matching the two-
character sequence "\n" (a backslash followed by a lowercase letter
"n"). Consider the string

$foo = '\n'

or,

$foo = "\\n"

The sequence "\n" is special in a regular expression, in that the two
characters stand for a linefeed character. Now, in a single-quoted
pattern, you must precede the "n" by three backslashes:

preg_match('`\\\n`',$foo)

The reason is that a backslash followed by another backslash results
in a single literal backslash in single-quoted strings. The Manual
isn't at all clear on this point IMO, although it does say as much,
In the section on preg_replace, or in another section on quoted strings?
in a roundabout kind of way. If we had just two backslashes, the
regular expression would only match a newline character, just as
would happen with a single backslash. (A single and double backslash
behave identically here because a single backslash followed by a
letter is not special in single-quoted strings, but a double
backslash is transformed into a single backslash.)

In a double-quoted pattern however, we need four backslashes before
the "n". This is because in double-quoted strings, a backslash
following a backslash results in a single literal backslash, just as
in single-quoted strings, but additionally, special "escape
sequences" are recognised within double-quoted strings. "\n" means a
linefeed character. To stop "\n" from having that meaning, we need to
escape the backslash with another backslash. Thus, we must use four
backslashes in total:

preg_match("`\\\\n`",$foo)
That's what I meant by more complicated.

Thanks for the detailed explanation!

Best,

S

--
Jock

Jul 17 '05 #5
sinister wrote:
"John Dunlop" <jo*********@johndunlop.info> wrote in message
news:MP************************@news.freeserve.net ...
Dunno. You might not be too wrong if you were to ascribe that to
inertia. Remember that examples are just examples -- illustrations of
particular methods, if you like -- and aren't intended as best-
practice guidelines. The Manual doesn't urge you to copy their
constructs character for character. It's sometimes necessary to
question authority. ;-)


That's an interesting aside. I understand that reference material cannot
dwell exclusively on best practices, but IMHO they ought to be emphasized.


I agree. I honestly don't know why they've used double-quoted strings
when they're not making use of the benefits that come with them.
The reason is that a backslash followed by another backslash results
in a single literal backslash in single-quoted strings. The Manual
isn't at all clear on this point IMO, although it does say as much,


In the section on preg_replace, or in another section on quoted strings?


In the section on single-quoted strings it says:

| To specify a literal single quote, you will need to escape it with
| a backslash (\), like in many other languages. If a backslash needs
| to occur before a single quote or at the end of the string, you
| need to double it. Note that if you try to escape any other
| character, the backslash will also be printed! So usually there is
| no need to escape the backslash itself.

http://www.php.net/manual/en/language.types.string.php

It says "Note that if you try to escape any other character, the
backslash will also be printed!". The prior sentence stated in what
position a backslash *must* be escaped (with another backslash). It
doesn't explicitly say that if you escape a backslash anywhere in a
single-quoted string, only a single backslash is left after parsing.
But that is what it's saying, implicitly. I'm fairly sure it could be
worded better. Maybe that's just me though.

--
Jock
Jul 17 '05 #6

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

Similar topics

11
by: Jakanapes | last post by:
Hi all, I'm looking for a way to scan a block of text and replace all the double quotes (") with single quotes ('). I'm using PHP to pull text out of a mySQL table and then feed the text into...
2
by: girish | last post by:
In my XML document, some node attributes data contains both single quot and double quote characters, such as <input msg="Hello "World", What's up"/>. The double quotes are in form of escape...
4
by: sankofa | last post by:
hi, i can't seem to be able to escape my single quote properly... is it even possible in javascript? this is a portion of my code.. var DLEWIS="Pastor Lewis"; .... Sermon is a yser-defined...
5
by: Joel | last post by:
Hi, I incorporated a function in my code that whenever I use a string variable in an sql statement if the string contains a single quote it will encase it in double quotes else single quotes. ...
4
by: Greg | last post by:
I keep getting an error when I have a tick mark in a text value that I am searching for in my XPath Query. Example: <Authors> <Author LastName="O'Donnel"> <Author LastName="Smith">...
3
by: Jason | last post by:
I have several tables with quite a few fields and I'm getting errors when trying to insert records with single quotes in the data like: name = John O'Henry or a city name of O'Fallen So I went...
7
by: gar | last post by:
Hi, I need to replace all the double quotes (") in a textbox with single quotes ('). I used this code text= Replace(text, """", "'" This works fine (for normal double quotes).The problem...
4
by: Justin Fancy | last post by:
Hi everyone, I need to replace all instances of a double quote(") with two single quotes('') in a text file. I already have some replacements of strings going on, but I tried this one, but the...
2
by: Reporter | last post by:
I got the following example from http://www.evolt.org/article/User_Friendly_Forms_in_PHP/20/60144/index.html : echo '<tr><td>First name:</td><td><input type="text" name="first_name"...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.