简体   繁体   中英

PHP Regular expression or str_replace and the string \201

I can't make this PHP to work:

$fo = "m\201 m\901";
echo str_replace('\\','*',$fo);
echo '<br>';
echo preg_replace('/\\\/', '%', $fo);

I get this:

m� m*901
m� m%901

As if, the \201 is a symbol for some strange thing?

Try single quotes

$fo = 'm\201 m\901';

A backslash followed by 3 octal numbers [0-7] in double quotes is PHP notation for a specific character. Check out the PHP manual . The character \201 or \x81 does not exist in the UTF-Code. We get a � when outputting. The 9 does not exist as an octal number, so \901 is not interpreted as a character.

In single quotes, no octal numbers are interpreted as characters. However, a backslash itself must be written as \\.

$fo = 'm\201 m\901';
$new = str_replace('\\','*',$fo);
var_dump($new);
//string(11) "m*201 m*901"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM