RegEx to match value of a variable or a string (with or without quotes)

前端 未结 2 578
南旧
南旧 2021-01-22 20:42

Here is my dilemma:

I wrote this RegEx pattern which works in my sandbox but does not work on my website:

Sandbox: http://regex101.com/r/vP3uG4

Pattern:<

2条回答
  •  太阳男子
    2021-01-22 21:04

    If the variable $value contains a numerical value then the replacement pattern in your preg_replace will look like this: $12$3

    That's true but not as you expected. In Regex Engine, $ffffd or here $dd (which are equal to \ffffd and \dd) are treated as octal numbers.

    So in this case $12 means a octal index 12 which is equal to a kind of space in ASCII.

    In the case of working with these tricky issues in Regular Expressions you should wrap your backreference number within {} so it should be ${1}2${3}

    Change your replacement pattern to '${1}'.$value.'${3}'

提交回复
热议问题