过滤XML中遇到的非法字符

XML规范规定的非法字符有:

/*
  0x00 - 0x08
  0x0b - 0x0c
  0x0e - 0x1f
*/

含有这些字符的XML文档在解析的时候就会报错,因此输出的时候需要进行过滤。

PHP的实现:

function XmlSafeStr($s)
{
  /*
    0x00 - 0x08
    0x0b - 0x0c
    0x0e - 0x1f
  */
  return preg_replace("/[x00-x08x0b-x0cx0e-x1f]/",'',$s);
}

当然,如果有现成的PHP模块提供类似的功能,请大家告诉我 :P

发表看法

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word