Skip to content

Instantly share code, notes, and snippets.

@sursir
Created August 22, 2017 04:39
Show Gist options
  • Save sursir/3fd850591de0c03603a4878fba930315 to your computer and use it in GitHub Desktop.
Save sursir/3fd850591de0c03603a4878fba930315 to your computer and use it in GitHub Desktop.
XML
<?php
/**
* 过滤 XML 中不合法的字符 否则将无法正常解析XML
*/
function XmlSafeFilter($s)
{
//XML标准规定的无效字节为:
/*
0×00 – 0×08 0 - 8
0x0b – 0x0c 11 - 12
0x0e – 0x1f 14 - 31
*/
// 16进制形式 直接匹配
// html10进制形式 由16进制转换为10进制 作为范围来筛查
// 16进制 与 10进制
$html16 = "[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]";
$html10 = "&#([0-8]|1[12]|1[4-9]|2[0-9]|3[01]);";
$regex = "/{$html16}|{$html10}/";
return preg_replace($regex, '', $s);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment