In single quoted strings only the escape sequences \\ and \' are recognized; any other occurrence of \ is interpreted as a plain character.

So since \M and \U are no valid escape sequences, they are interpreted as they are.

Answer from Gumbo on Stack Overflow
๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ function.addslashes.php
PHP: addslashes - Manual
Returns a string with backslashes added before characters that need to be escaped. These characters are: ... A use case of addslashes() is escaping the aforementioned characters in a string that is to be evaluated by PHP:
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ php_string_escape.asp
PHP - Escape Characters
To insert characters that are illegal in a string, use an escape character. In PHP, an escape character is a backslash \ followed by the character you want to insert.
๐ŸŒ
Beamtic
beamtic.com โ€บ excaping-backslash-php
Escaping Backslash in PHP | Beamtic
December 26, 2020 - When performing a replacement operation, we can use preg_quote for the pattern, but this can not be used for the replacement string; instead we need to escape each literal backslash with another backslash. $replacement_id = 'REPLACEMENT_ID'; // Escape backslashes in PHP script (First level) $unescaped_string = 'c:\\test\\testing\\some-file.txt'; // Escape backslashes in the replacement string (Second level) $escaped_string = str_replace('\\', '\\\\', $unescaped_string); $new_html = preg_replace('|{$replacement_id}|', $escaped_string, $html);
๐ŸŒ
BUSoft Technologies
busofttech.com โ€บ home โ€บ php escape quotes how to do it right?
PHP Escape Quotes: How to Do It Right | BUSoftTech
March 10, 2026 - The special case is that if you to display a literal single-quote, escape it with a backslash(\) and if you want to display a backslash, you can escape it with another backslash(\\). ... <?php // A Simple String echo โ€˜I am a developer.โ€™; ...
Find elsewhere
๐ŸŒ
Hacking with PHP
hackingwithphp.com โ€บ 2 โ€บ 6 โ€บ 2 โ€บ escape-sequences
Escape sequences โ€“ Hacking with PHP - Practical PHP
<?php $MyString = "This is an \"escaped\" string"; $MySingleString = 'This \'will\' work'; $MyNonVariable = "I have \$zilch in my pocket"; $MyNewline = "This ends with a line return\n"; $MyFile = "c:\\windows\\system32\\myfile.txt"; ?> It is particularly common to forget to escape Windows-style file system paths properly, but as you can see, it is simply a matter of adding more backslashes in there.
๐ŸŒ
PHPpot
phppot.com โ€บ php โ€บ php-escape-sequences
PHP Escape Sequences: Quotes, Newlines and Tabs - PHPpot
August 15, 2026 - For example, \n creates a newline, \t creates a tab, and \" inserts a double quote. Single-quoted strings are much simpler. PHP normally recognizes only \' for a single quote and \\ for a backslash.
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ func_string_addslashes.asp
PHP addslashes() Function
zip_close() zip_entry_close() ... zip_open() zip_read() PHP Timezones ... The addslashes() function returns a string with backslashes in front of predefined characters....
๐ŸŒ
GitHub
github.com โ€บ Hamz-a โ€บ php-regex-best-practices โ€บ blob โ€บ master โ€บ 06 Escaping a backslash hell.md
php-regex-best-practices/06 Escaping a backslash hell.md at master ยท Hamz-a/php-regex-best-practices
Notice that you don't need to escape the brackets inside the regex. You could see the first braces as "group 0" and the second (inner braces) as group 1. However, the opinions are divided about its usage. Some would endorse it and some would avoid it as it might seem confusing. Sometimes you need to match double quotes, use single quotes for defining the regex string and vice versa: // Backslash $regex = "\"([^\"]+)\""; $regex = '\'([^\']+)\''; // Clean $regex = '"([^"]+)"'; $regex = "'([^']+)'";
Author: Hamz-a
๐ŸŒ
FlatCoding
flatcoding.com โ€บ home โ€บ php escape characters: how to escape special characters
PHP Escape Characters: How to Escape Special Characters - FlatCoding
April 25, 2025 - When you want to include a backslash itself, use \\ because a single backslash starts an escape sequence. Escape characters prevent errors when the string contains special symbols.
๐ŸŒ
BCCNsoft
doc.bccnsoft.com โ€บ docs โ€บ php-docs-7-en โ€บ function.addslashes.html
Quote string with slashes
Returns the escaped string. ... <?php $str = "Is your name O'Reilly?"; // Outputs: Is your name O\'Reilly?
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ how-to-use-escape-characters-in-php
How to use Escape Characters in PHP ? - GeeksforGeeks
July 12, 2024 - Escape characters are handy for introducing newlines (\n) and tabs (\t) within strings. ... If you need to include a backslash itself within a string, you can use \\.
Top answer
1 of 6
63
// PHP 5.4.1

// Either three or four \ can be used to match a '\'.
echo preg_match( '/\\\/', '\\' );        // 1
echo preg_match( '/\\\\/', '\\' );       // 1

// Match two backslashes `\\`.
echo preg_match( '/\\\\\\/', '\\\\' );   // Warning: No ending delimiter '/' found
echo preg_match( '/\\\\\\\/', '\\\\' );  // 1
echo preg_match( '/\\\\\\\\/', '\\\\' ); // 1

// Match one backslash using a character class.
echo preg_match( '/[\\]/', '\\' );       // 0
echo preg_match( '/[\\\]/', '\\' );      // 1  
echo preg_match( '/[\\\\]/', '\\' );     // 1

When using three backslashes to match a '\' the pattern below is interpreted as match a '\' followed by an 's'.

echo preg_match( '/\\\\s/', '\\ ' );    // 0  
echo preg_match( '/\\\\s/', '\\s' );    // 1  

When using four backslashes to match a '\' the pattern below is interpreted as match a '\' followed by a space character.

echo preg_match( '/\\\\\s/', '\\ ' );   // 1
echo preg_match( '/\\\\\s/', '\\s' );   // 0

The same applies if inside a character class.

echo preg_match( '/[\\\\s]/', ' ' );   // 0 
echo preg_match( '/[\\\\\s]/', ' ' );  // 1 

None of the above results are affected by enclosing the strings in double instead of single quotes.

Conclusions:
Whether inside or outside a bracketed character class, a literal backslash can be matched using just three backslashes '\\\' unless the next character in the pattern is also backslashed, in which case the literal backslash must be matched using four backslashes.

Recommendation:
Always use four backslashes '\\\\' in a regex pattern when seeking to match a backslash.

Escape sequences.

2 of 6
19

To avoid this kind of unclear code you can use \x5c Like this :)

echo preg_replace( '/\x5c\w+\.php$/i', '<b>${0}</b>', __FILE__ );