Home  >  Q&A  >  body text

The matches in () will be put into the array. The question is what is the difference between $1 and \\1?

ubb text editing, preg_replace($pattern,$replace,$stirng);

争渡争渡2538 days ago1704

reply all(2)I'll reply

  •  

     2018-01-29 15:54:48

    I read in the manual that \\1 and $1 are the same.

    Original words from the manual: Replacement can contain back references \\n or $n, and the latter is preferred grammatically. Each such reference will be replaced by the text captured by the matching nth capturing subgroup. n can be 0-99, \\0 and $0 represent the complete pattern matching text. The serial number counting method of capturing subgroups is: the left bracket representing the capturing subgroup is counted from left to right, starting from 1. If you want to use backslashes in replacement, you must use 4 ("\\\\", translator's annotation: Because this is first a PHP string, after escaping, there are two, and then after passing through the regular expression engine is considered a text backslash).

    reply
    0
  •  

    If we talk about the difference, it is: preg_replace() We don’t know whether we want to express \\11 or \\1 1 (the second 1 is the string 1). So using ${1}1 can let preg_replace() know that the first 1 is a backreference, and the second 1 is just the 1 of the original text immediately following the backreference.

      · 2018-01-29 16:00:36
  • Cancelreply