php符转换为HTML实体

  1. function d_htmlspecialchars($string) {
  2. if(is_array($string)) {
  3. foreach($string as $key => $val) {
  4. $string[$key] = d_htmlspecialchars($val);
  5. }//开源代码phpfensi.com
  6. } else {
  7. $string = str_replace('&', '&', $string);
  8. $string = str_replace('"', '"', $string);
  9. $string = str_replace(''', ''', $string);
  10. $string = str_replace('<', '&lt;', $string);
  11. $string = str_replace('>', '&gt;', $string);
  12. $string = preg_replace('/&amp;(#d;)/', '&1', $string);
  13. }
  14. return $string;
  15. }