简体   繁体   English

在PHP中替换多个utf-8字符

[英]replacing multiple utf-8 characters in php

I have a string with multiple utf-8 characters that look like this 我有一个包含多个utf-8字符的字符串,看起来像这样

\u00b4, \u2019, \u201b, \u2032

I want to replace these with the following html character 我想用以下HTML字符替换它们

'

I'm using the following php code to replace these 我正在使用以下php代码替换这些

$search  = "(\\u00b4|\\u2019|\\u201b|\\u2032)"; 
$replace = "'";

$result = preg_replace($search, $replace, $string);

I keep getting the following warning, and $result is null 我不断收到以下警告,并且$ result为null

Warning:  preg_replace(): Compilation failed: PCRE does not support \\L, \\l, \\N, \\U, or \\u at offset 2 in /...

I have no idea what to do. 我不知道该怎么做。 Any ideas on how to proceed with replacing these utf8 characters is appreciated! 任何有关如何替换这些utf8字符的想法都值得赞赏!

$unicode = "\u00b4 \u2019 \u201b \u2032";
$unicode = preg_replace('/\\\\u[^ ]+/im', "'\r\n", $unicode);
echo $unicode;

You didn't escape the backslashes correctly, you need 2 extra backslashes: 您没有正确地转义反斜杠,您需要再加上2个反斜杠:

\\\\

对特定的字符代码进行预匹配时,您需要使用\\ x十六进制表示法,而不是unicode表示法-看起来像unicode值。

$search  = "(\xb4|\x2019|\x201b|\x2032)"; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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