如何使用PHP跳过XML文件中的无效字符

use*_*396 22 php xml utf-8

我正在尝试使用PHP解析XML文件,但是我收到一条错误消息:

解析器错误:字符0x0超出允许范围

我认为这是因为XML的内容,我认为有一个特殊符号"☆",任何想法我能做些什么来解决它?

我也得到:

解析器错误:标记项行中数据的过早结束

可能导致该错误的原因是什么?

我正在使用simplexml_load_file.

更新:

我尝试找到错误行并将其内容粘贴为单个xml文件,它可以工作!所以我仍然无法弄清楚是什么让xml文件解析失败.PS它是一个超过100M的巨大xml文件,它会解析错误吗?

Jho*_*ong 34

你有控制XML吗?如果是这样,请确保数据包含在<![CDATA[.. ]]>块中.

而且你还需要清除无效字符:

/**
 * Removes invalid XML
 *
 * @access public
 * @param string $value
 * @return string
 */
function stripInvalidXml($value)
{
    $ret = "";
    $current;
    if (empty($value)) 
    {
        return $ret;
    }

    $length = strlen($value);
    for ($i=0; $i < $length; $i++)
    {
        $current = ord($value{$i});
        if (($current == 0x9) ||
            ($current == 0xA) ||
            ($current == 0xD) ||
            (($current >= 0x20) && ($current <= 0xD7FF)) ||
            (($current >= 0xE000) && ($current <= 0xFFFD)) ||
            (($current >= 0x10000) && ($current <= 0x10FFFF)))
        {
            $ret .= chr($current);
        }
        else
        {
            $ret .= " ";
        }
    }
    return $ret;
}
Run Code Online (Sandbox Code Playgroud)

  • 在这种情况下,不确定这是否有用.你无法解决CDATA的编码问题,只能解决像"&"而不是"&amp;"这样的问题. (3认同)
  • 这个功能坏了。`ord()` 仅对单字节进行操作。 (3认同)

mik*_*wn2 13

我决定测试所有UTF-8值(0-1114111),以确保工作正常.使用preg_replace()会导致在测试所有utf-8值时由于错误而返回NULL.这是我提出的解决方案.

$utf_8_range = range(0, 1114111);
$output = ords_to_utfstring($utf_8_range);
$sanitized = sanitize_for_xml($output);


/**
 * Removes invalid XML
 *
 * @access public
 * @param string $value
 * @return string
 */
function sanitize_for_xml($input) {
  // Convert input to UTF-8.
  $old_setting = ini_set('mbstring.substitute_character', '"none"');
  $input = mb_convert_encoding($input, 'UTF-8', 'auto');
  ini_set('mbstring.substitute_character', $old_setting);

  // Use fast preg_replace. If failure, use slower chr => int => chr conversion.
  $output = preg_replace('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u', '', $input);
  if (is_null($output)) {
    // Convert to ints.
    // Convert ints back into a string.
    $output = ords_to_utfstring(utfstring_to_ords($input), TRUE);
  }
  return $output;
}

/**
 * Given a UTF-8 string, output an array of ordinal values.
 *
 * @param string $input
 *   UTF-8 string.
 * @param string $encoding
 *   Defaults to UTF-8.
 *
 * @return array
 *   Array of ordinal values representing the input string.
 */
function utfstring_to_ords($input, $encoding = 'UTF-8'){
  // Turn a string of unicode characters into UCS-4BE, which is a Unicode
  // encoding that stores each character as a 4 byte integer. This accounts for
  // the "UCS-4"; the "BE" prefix indicates that the integers are stored in
  // big-endian order. The reason for this encoding is that each character is a
  // fixed size, making iterating over the string simpler.
  $input = mb_convert_encoding($input, "UCS-4BE", $encoding);

  // Visit each unicode character.
  $ords = array();
  for ($i = 0; $i < mb_strlen($input, "UCS-4BE"); $i++) {
    // Now we have 4 bytes. Find their total numeric value.
    $s2 = mb_substr($input, $i, 1, "UCS-4BE");
    $val = unpack("N", $s2);
    $ords[] = $val[1];
  }
  return $ords;
}

/**
 * Given an array of ints representing Unicode chars, outputs a UTF-8 string.
 *
 * @param array $ords
 *   Array of integers representing Unicode characters.
 * @param bool $scrub_XML
 *   Set to TRUE to remove non valid XML characters.
 *
 * @return string
 *   UTF-8 String.
 */
function ords_to_utfstring($ords, $scrub_XML = FALSE) {
  $output = '';
  foreach ($ords as $ord) {
    // 0: Negative numbers.
    // 55296 - 57343: Surrogate Range.
    // 65279: BOM (byte order mark).
    // 1114111: Out of range.
    if (   $ord < 0
        || ($ord >= 0xD800 && $ord <= 0xDFFF)
        || $ord == 0xFEFF
        || $ord > 0x10ffff) {
      // Skip non valid UTF-8 values.
      continue;
    }
    // 9: Anything Below 9.
    // 11: Vertical Tab.
    // 12: Form Feed.
    // 14-31: Unprintable control codes.
    // 65534, 65535: Unicode noncharacters.
    elseif ($scrub_XML && (
               $ord < 0x9
            || $ord == 0xB
            || $ord == 0xC
            || ($ord > 0xD && $ord < 0x20)
            || $ord == 0xFFFE
            || $ord == 0xFFFF
            )) {
      // Skip non valid XML values.
      continue;
    }
    // 127: 1 Byte char.
    elseif ( $ord <= 0x007f) {
      $output .= chr($ord);
      continue;
    }
    // 2047: 2 Byte char.
    elseif ($ord <= 0x07ff) {
      $output .= chr(0xc0 | ($ord >> 6));
      $output .= chr(0x80 | ($ord & 0x003f));
      continue;
    }
    // 65535: 3 Byte char.
    elseif ($ord <= 0xffff) {
      $output .= chr(0xe0 | ($ord >> 12));
      $output .= chr(0x80 | (($ord >> 6) & 0x003f));
      $output .= chr(0x80 | ($ord & 0x003f));
      continue;
    }
    // 1114111: 4 Byte char.
    elseif ($ord <= 0x10ffff) {
      $output .= chr(0xf0 | ($ord >> 18));
      $output .= chr(0x80 | (($ord >> 12) & 0x3f));
      $output .= chr(0x80 | (($ord >> 6) & 0x3f));
      $output .= chr(0x80 | ($ord & 0x3f));
      continue;
    }
  }
  return $output;
}
Run Code Online (Sandbox Code Playgroud)

并在一个简单的对象或数组上执行此操作

// Recursive sanitize_for_xml.
function recursive_sanitize_for_xml(&$input){
  if (is_null($input) || is_bool($input) || is_numeric($input)) {
    return;
  }
  if (!is_array($input) && !is_object($input)) {
    $input = sanitize_for_xml($input);
  }
  else {
    foreach ($input as &$value) {
      recursive_sanitize_for_xml($value);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)


nwe*_*hof 6

某些 Unicode 字符不得出现在 XML 1.0 中

\n
    \n
  • C0 控制代码 (U+0000 - U+001F) 除了制表符、CR 和 LF。
  • \n
  • UTF-16 代理项 (U+D800 - U+DFFF)。这些在 UTF-8 中也是无效的,并且在遇到时表明存在更严重的问题。
  • \n
  • U+FFFE 和 U+FFFF。
  • \n
\n

但在实践中,您经常必须处理从包含此类字符的其他源中不小心生成的 XML。如果您想处理 UTF-8 编码字符串中无效 XML 的这种特殊情况,我建议:

\n
$str = preg_replace(\n    \'/[\\x00-\\x08\\x0B\\x0C\\x0E-\\x1F]|\\xED[\\xA0-\\xBF].|\\xEF\\xBF[\\xBE\\xBF]/\',\n    "\\xEF\\xBF\\xBD",\n    $str\n);\n
Run Code Online (Sandbox Code Playgroud)\n

这不使用uUnicode 正则表达式修饰符,而是直接作用于 UTF-8 编码字节以获得额外的性能。该模式的部分是:

\n
    \n
  • 无效的控制字符:[\\x00-\\x08\\x0B\\x0C\\x0E-\\x1F]
  • \n
  • UTF-16 代理:\\xED[\\xA0-\\xBF].
  • \n
  • 非字符 U+FFFE 和 U+FFFF:\\xEF\\xBF[\\xBE\\xBF]
  • \n
\n

无效字符将替换为替换字符 U+FFFD (\xef\xbf\xbd),而不是简单地删除它们。这使得诊断无效字符变得更加容易,甚至可以防止安全问题

\n