PHP正则表达式破坏特殊字符 [英] PHP regex breaking special characters

查看:187
本文介绍了PHP正则表达式破坏特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var_dump(
  preg_replace( 
    array( 
      '#[\s\n\\n]*<[\/\s]?(br|/p|/div)[\/\s]?>[\s\n\\n]*#iu', '#\s+#' ), 
      ' ', "A 19th century Dutch walnut secrétaire à abattant") 
    )
  )
); 

结果是:

string(102) "A 19th century Dutch walnut secrétaire � abattant" 

为什么我的正则表达式在PHP中打破特殊字符?

Why does my Regex breaking special character in PHP?

推荐答案

如果你设置你的字符编码,没有问题。查看您在 eval.in 上运行的代码。

If you set your character encoding right, there is no issue. See your code run on eval.in.

Make一定要在您的html文档中有这个:

Make sure to have this in your html document:

<!DOCTYPE html>
<html>
<head>
     <meta charset="utf-8">
</head>

并将PHP文件保存为UTF-8编码。根据您的编辑器,这可能是另存为对话框或首选项或选项菜单下的一个选项。

And also save your PHP file as UTF-8 encoded. Depending on your editor this might be an option in the "Save As" dialog, or under the "preferences" or "options" menu.

这篇关于PHP正则表达式破坏特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆