Php 如何解析单引号字符串中的字符?

Php 如何解析单引号字符串中的字符?,php,hex,quotes,Php,Hex,Quotes,要正确解析双引号字符串(我无法更改),我必须执行以下操作: $string = '15 Rose Avenue\n Irlam\n Manchester'; $string = str_replace('\n', "\n", $string); print nl2br($string); // demonstrates that the \n's are now linebreak characters 到目前为止,一切顺利。 但是在我给定的字符串中有类似于\xC3\xA4的字符。有许多这样

要正确解析双引号字符串(我无法更改),我必须执行以下操作:

$string = '15 Rose Avenue\n Irlam\n Manchester'; 
$string = str_replace('\n', "\n", $string);
print nl2br($string); // demonstrates that the \n's are now linebreak characters
到目前为止,一切顺利。 但是在我给定的字符串中有类似于
\xC3\xA4
的字符。有许多这样的字符(以\x…开头)
如何使用换行符正确解析它们,如上图所示?

您可以用单引号转义
\

$string = str_replace('\\n', "\n", $string);
但是如果您需要执行
\\xC3
等操作,您将有很多潜在的替代品。。。。最好使用带有函数(回调)的
preg\u replace\u callback()

$str = stripcslashes($str);

美好的还可以提供指向的链接,并指出此处的所有“\xYY”都是十六进制字符表示法,该方法也可以处理类似C的\n\r。。。和八进制表示法。