Viewed   48 times

When testing an answer for another user's question I found something I don't understand. The problem was to replace all literal t n r characters from a string with a single space.

Now, the first pattern I tried was:


which surprisingly didn't work. I tried the same pattern in Perl and it worked fine. After some trial and error I found that PHP wants 3 or 4 backslashes for that pattern to match, as in:




these patterns - to my surprise - both work. Why are these extra backslashes necessary?



You need 4 backslashes to represent 1 in regex because:

  • 2 backslashes are used for unescaping in a string ("\\" -> \)
  • 1 backslash is used for unescaping in the regex engine (\ -> )

From the PHP doc,

escaping any other character will result in the backslash being printed too1

Hence for \[,

  • 1 backslash is used for unescaping the , one stay because [ is invalid ("\[" -> \[)
  • 1 backslash is used for unescaping in the regex engine (\[ -> [)

Yes it works, but not a good practice.

Tuesday, August 16, 2022

Thank everybody for help.

My solution based on 'bobbogo' solution. Thank you.

Regular expression:


Result (from RegexBuggy):

4 XXooYYuuXX_ZZ     XXooYYuuXX_ZZ

Possible it can by more optimized? I am not big professional in regex.

Saturday, November 26, 2022

There are couple of problems:

  1. Your regex pattern will also match an input of more than 15 characters.
  2. Your regex will also other non-allowed characters in the middle like @ or # due to use of S

You can fix it by using a negative lookahead to disallow consecutive occurrence of period/hyphen/underscore and remove S from middle of regex that allows any non-space character


RegEx Demo

Monday, August 15, 2022

(backslash) is the namespace separator in PHP 5.3.

A before the beginning of a function represents the Global Namespace.

Putting it there will ensure that the function called is from the global namespace, even if there is a function by the same name in the current namespace.

Wednesday, October 19, 2022

Instead of meddling with preg_replace(), consider using var_export() to print a "true copy" of the string:

foreach ($strings as $s) {
    echo var_export($s, true), PHP_EOL;


'this is a simple string'
'Arnold once said: "I'll be back"'
'You deleted C:\*.*?'
'You deleted C:\*.*?'
'This will not expand: \n a newline'
'Variables do not $expand $either'

As you can see, sentence 3 and 4 are identical to PHP.

Wednesday, October 26, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :