Simple Quotes vs. Doubles in PHP Regex

4

Considering that I have a path which I want to check with preg_match if it is the desired path, what is the difference between the two following operations?

// 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)
    
asked by anonymous 18.10.2016 / 14:38

2 answers

3

The Ivcs answer 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 just one here '/\/path\/to\/folder\//' .

What you want

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

Explanation

  • 1st problem: This is the very assembly of RegEx, since you are using% as%. So every time you want to use a / literal in RegEx, you will have to escape to not be interpreted as an end of expression, so you need / when single quotation marks \/ and ' when double quotation marks \/ .

  • 2nd problem: Within double quotation marks, " is an escape character, so everything that comes after \ will be converted to literal. So your regex that was \ will become "/\/path\/to\/folder\//" generating an error in RegEx compilation.

Solutions

  • Keep the "//path/to/folder//" border and escape every / : \
  • Change to single quotation mark, so it does not interpret "/\/path\/to\/folder\//" as escape from the string but literal: \
  • Use another border character, such as '/\/path\/to\/folder\//' , note that here you still have ~ as escape string: \
  • Summarizing everything

    '~/path/to/folder/~'
    
    • Here you do not have to escape the characters equal to the borders.
    • "~\/path\/to\/folder\/~" does not have to be escaped with / .
    18.10.2016 / 15:44
    3

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

    Double quotes:

    • A little slower, so try to process data

    • read variables within it, eg "hi $ name"

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

    Single quotes:

    • A bit faster, because it is treated as a string, regardless of whether there are any escape or variables present in it.

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

    In the first example:

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

    It would be:

    /\/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 in single quotation marks, there is treatment, which in this case would be the characters of escapes, so the string would look like it was previously.

        
    18.10.2016 / 14:45