简体   繁体   English

正则表达式替换 PHP 中的特殊字符?

[英]Regex to replace a special character in PHP?

I have problem with a special character § .我有特殊字符§ I want to replace multiple occurrences of § with single § .我想替换多次出现§§ The following regex works fine on Regex 101 .以下正则表达式在Regex 101上运行良好。

$file_data = file_get_contents($file_name);
$file_data = preg_replace('/\§+/g', '§',$file_data);

It changed它变了

§§§§§§§§§This free 3D robot game could redefine how kids learn to codeDigital Trends It's hard to get kids to code. §§§§§§§§§这款免费的 3D 机器人游戏可以重新定义孩子们学习编码的方式Digital Trends 让孩子们编码很难。 Up until very recently, it was largely ....直到最近,它主要是......

to

§This free 3D robot game could redefine how kids learn to codeDigital Trends It's hard to get kids to code. §这款免费的 3D 机器人游戏可以重新定义孩子们学习编码的方式数字趋势让孩子们编写代码很难。 Up until very recently, it was largely ....直到最近,它主要是......

However, it is not working on the server after I upload it.但是,我上传后它在服务器上不起作用。 Here is the var_dump($file_data) by PHP这是 PHP 的var_dump($file_data)

§§§§§§§§ This free 3D robot game could redefine how kids learn to codeDigital Trends It's hard to get kids to code. §§§§§§§§§§§§ 这款免费的 3D 机器人游戏可以重新定义孩子们学习编码的方式Digital Trends 让孩子们编码很难。 Up until very recently, it was largely ....直到最近,它主要是......

So, there seems to be an additional character  before every § in the var_dump .因此,在var_dump每个§之前似乎都有一个额外的字符 The extra character  does not show up on webpage when echoed as HTML.当作为 HTML 回显时,额外的字符Â不会显示在网页上。 It just shows up during plain PHP var_dump .它只是在普通的 PHP var_dump期间出现。 How can I replace multiple occurrences of § using regex in PHP?如何在 PHP 中使用正则表达式替换多次出现的§

You will need to set the u (utf-8) modifier:您需要设置u (utf-8) 修饰符:

From perlre documentation:从 perlre 文档:

/u means to use Unicode rules when pattern matching. /u表示在模式匹配时使用 Unicode 规则。 On ASCII platforms, this means that the code points between 128 and 255 take on their Latin-1 (ISO-8859-1) meanings (which are the same as Unicode's)....在 ASCII 平台上,这意味着 128 和 255 之间的代码点采用它们的 Latin-1 (ISO-8859-1) 含义(与 Unicode 相同)......

$output = preg_replace('/§+/u', '§', $input);
                         // ^ 
$str="§§§§§§§§§This free 3D robot game could redefine how kids learn to codeDigital Trends It's hard to get kids to code. Up until very recently, it was largely ....";
$pttn='@\§{2,}@um';
echo preg_replace( $pttn,'§',$str );

/* will output */
/*
   §This free 3D robot game could redefine how kids learn to codeDigital Trends It's hard to get kids to code. Up until very recently, it was largely .... 
*/

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

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