Skip to content
Advertisement

php preg_replace strings with brackets

I am trying to replace some text if it isn’t preceded with a dot (.), but my function seems to result include an unexpected slash …

JavaScript

A sample of this code can be found at http://sandbox.onlinephpfunctions.com/code/24cab4eece20d22a11dd887da44d63e393b51aa9

which outputs…

JavaScript

but I want the output to read…

JavaScript

Where am I going wrong?

Advertisement

Answer

It’s because in this line, you’re adding the backslash and then never remove it

JavaScript

You could change your code to the following so that you don’t need to manually escape this bracket.

JavaScript

However, you could get the same output with much less code

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement