Single vs. Double Quotes in PHP Regex

Asked

Viewed 480 times

4

whereas I own a path which I wish to verify with preg_match if it is the desired path, what is the difference between the two operations to follow?

// Single quoted string
preg_match('/\/path\/to\/folder\//', $path)

// Double quoted string
preg_match("/\/path\/to\/folder\//", $path)

Assuming also any string:

// Single quoted string
preg_match('/some\\\\string/', $string)

// Double quoted string
preg_match("/some\\\\string/", $string)

2 answers

3

Using single or double quotes comes from the PHP language, usually their characteristics are:

Double quotes:

  • a little slower as it tries to process data

  • reads variables enter it, example: "hi $name"

  • allows escape characters such as: "hi on another line"

Single quotes:

  • a little faster, as it is treated as a string, regardless of whether there are escapes or variables present in it.

The only difference that can occur is that by giving simple quotes you will not have the character and escape ' ', then it would be safer to use double quotes.

In the first example:

// Single quoted string
preg_match('/\/path\/to\/folder\//', $path)

// Double quoted string
preg_match("/\/path\/to\/folder\//", $path)

Seria:

/\/path\/to\/folder\//

//path/to/folder//

In short, PHP will treat the string according to the quotes used, in the first nothing changes because it does not perform any treatment, as the second is between simple quotes, there is treatment, in case it would be the characters of escapes, then the string would be the form previously passed.

3


To response from the Ivcs explains very well the part of the quotes, just complementing the point that generates your problem.

Problem

  • You have a double problem here: "/\/path\/to\/folder\//".
  • And only one here '/\/path\/to\/folder\//'.

What do you want

  • Find the path, /path/to/folder/.

Explanation

  • first problem: This is the Regex assembly itself, as you are using as "borders" the /. So every time you want to wear one / literal in Regex, will have to escape not to be interpreted as end of expression, so need to \/ when single quotes ' and \\/ when double quotes ".

  • second problem: In double quotes, the \ is an escape character, so anything that comes after \ will be converted to literal. So his regex that was "/\/path\/to\/folder\//" will turn "//path/to/folder//" generating an error in the Regex build.

Solutions

  1. Keep the border / and escape every \: "/\\/path\\/to\\/folder\\//"
  2. Change to single quote, so it does not interpret the \ as escape from the string and yes as literal: '/\/path\/to\/folder\//'
  3. Use another border character, such as ~, note that here you still have \ as escape from the string: "~\/path\/to\/folder\/~"

Summing up

'~/path/to/folder/~'
  • Here you do not have to escape the characters equal to the borders.
  • Here to / does not need to be escaped with \.
  • 1

    Man, perfect, explains exactly what will be the Regex and still a toast a Good Practice.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.