marc-mabe/entity-coder

此包已被放弃,不再维护。未建议替代包。

编码和解码 XML、HTML 以及自定义实体到不同的字符集

dev-master 2021-02-07 19:44 UTC

This package is auto-updated.

Last update: 2021-02-07 19:45:14 UTC


README

!!! 已存档 !!!

此存储库已存档,没有任何替代品

编码和解码 XML、HTML 以及自定义实体到不同的字符集。

特性

  • 解码和编码十六进制实体
  • 解码和编码十进制实体
  • 从一种字符集转换为另一种字符集
  • 可选择是否使用命名实体(编码时使用)
  • 可选择是否使用十六进制或十进制实体(编码时使用)
  • 可选择如何处理无法转换为给定输出字符集的字符/实体
  • 可选择如何处理无效字符
  • 操作
    • 抛出异常
    • 调用回调函数
    • 忽略
    • 用替换字符替换
    • 使用实体
    • 通过 ASCII 名称进行转写

用法

编码

$entityCoder = new EntityCoder(array(
    // set named entity reference
    // -> 'special', 'xml', 'html' or an array of your own entities
    'entityReference' => 'special',

    // set your input charset (default: ISO-8859-1)
    'inputCharSet' => 'UTF-8',

    // set your output charset (default: ISO-8859-1)
    'outputCharSet' => 'ISO-8859-15',

    // convert to hex entities if no named entity exists (default: false)
    'hex' => true,
));

$text = $entityCoder->encode($text);

解码

$entityCoder = new EntityCoder(array(
    // set named entity reference
    // -> 'special', 'xml', 'html' or an array of your own entities
    'entityReference' => 'html',

    // set your input charset (default: ISO-8859-1)
    'inputCharSet' => 'ISO-8859-15',

    // set your output charset (default: ISO-8859-1)
    'outputCharSet' => 'ASCII',

    // How to handle invalid characters
    // EXCEPTION  = throw an exception
    // CALLBACK   = call a callback
    // IGNORE     = replace by an empty character
    // SUBSTITUTE = substitute by an substitution character (default: "?")
    // TRANSLIT_* = convert to ASCII name with a fallback to one of the other actions
    'invalidCharAction' => EntityCoder::ACTION_SUBSTITUTE,

    // How to handle invalid entities
    // EXCEPTION  = throw an exception
    // CALLBACK   = call a callback
    // IGNORE     = replace by an empty character
    // SUBSTITUTE = substitute by an substitution character (default: "?")
    // ENTITY     = leave the entity as-is
    'invalidEntityAction' => EntityCoder::ACTION_ENTITY,
));

$text = $entityCoder->decode($text);

要求

此库需要 PHP 5.3.0 或更高版本。另外需要 php 扩展 iconv(默认启用)。

问题和反馈

如果您在此版本中发现代码表现异常或与文档描述的行为不符,请在 GitHub 问题跟踪器中创建问题:

https://github.com/marc-mabe/entity-coder/issues

许可证

此存档中的文件在新 BSD 许可证下发布。您可以在 LICENSE.txt 中找到此许可证的副本。