gavinggordon/htmlentities

一个PHP类,它允许解码和编码比标准htmlentities和html_entity_decode函数更广泛的字符。

4.1 2016-05-14 00:20 UTC

This package is not auto-updated.

Last update: 2024-09-26 00:33:09 UTC


README

Build Status Dependency Status Total Packagist Downloads Usage License

描述

自PHP4以来,编码和解码被称为“Html实体”的字符集的能力就存在了。在PHP内置的众多函数中,有4个几乎相同的函数用于编码和解码HTML实体;然而,尽管它们相似,其中两个提供了其他函数没有的额外功能。

¹ htmlentities和html_entity_decode只能编码和解码PHP的HTML翻译表中的字符。

² htmlspecialchars和htmlspecialchars_decode只能编码和解码特殊字符³。

³ 特殊字符不会被解释为HTML标签,8位字符仅被编码为ASCII字符。

独特之处

这个类与众不同的地方在于,除了能够编码和解码PHP的htmlentities()和html_entity_decode()函数能够编码和解码的所有字符和实体之外,它还能够编码和解码大量PHP内置htmlentities编码和解码函数不会编码/解码的字符/实体,因为这些字符/实体在HTML中不具有特殊含义,如

  • 标点符号;
  • ASCII字符;
  • 希腊字符;
  • 拉丁字符;
  • 俄文字符;
  • 算术字符;
  • 等等...

使用方法

安装(通过Composer)

    composer require gavinggordon/htmlentities

示例

实例化

	include_once( __DIR__ . '/vendor/autoload.php' );

    $htmlentities = new \GGG\HtmlEntities();

编码

    $to_encode = 'Test-,;:';
    // Set a variable containing a string of the encoded characters you wish to be encoded;
    
    $encoded = $htmlentities->encode( $to_encode );
    // Get the encoded result by using the encode method on the returned instance of HtmlEntities;
    
    echo $encoded;
    // Display the encoded result, which is of type String;
    // Test‐,;:

解码

   $to_decode = 'Test˜*(#';
   // Set a variable containing a string of the encoded characters you wish to be decoded;
   
   $decoded = $htmlentities->decode( $to_decode );
   // Get the decoded result by using the decode method on the returned instance of HtmlEntities;
   
   echo $decoded;
   // Display the decoded result, which is of type String;
   // Test~*(#

更多信息

CodeClimate

点击此处查看我们的CodeClimate统计信息:点击这里

PHP创新奖

这个获得了PHP创新奖,由PHPClasses.org提供。我的其他PHP类可以通过我的GitHub个人资料PHPClasses.org个人资料在线访问。