PHP regex breaking special characters

和自甴很熟 提交于 2019-12-12 03:02:38

问题


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") 
    )
  )
); 

The result is:

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

Why does my Regex breaking special character in PHP?


回答1:


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

Make sure to have this in your html document:

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

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.



来源:https://stackoverflow.com/questions/37751209/php-regex-breaking-special-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!