简体   繁体   中英

PHP regex breaking special characters

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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