emirror-de / html-entities-named-numeric-mapping
提供从命名到数字HTML实体映射。
v0.1
2020-02-18 15:03 UTC
This package is not auto-updated.
Last update: 2024-09-26 11:47:54 UTC
README
一个简单的类,能够将命名的HTML实体转换为数字实体。
特别适用于将json数据存储在HTML数据属性中,因为使用JavaScript转换成字符非常容易。
版本4.01已从StackOverflow上的这个答案复制而来。感谢hakre分享他的代码。
使用方法
PHP
<?php
use W3C\HTMLEntities;
$named_entities = htmlentities("<>");
$numeric_entities = preg_replace_callback(
"/(&[a-z]+;)/",
function($m) {
return HTMLEntities::getNumeric($m[0]);
},
$named_entities
);
JavaScript
htmlEntitiesDecode = function(str) {
return String(str).replace(/\&#([0-9]){1,4};/gim, (i) => {
return String.fromCharCode(i.substr(2, (i.length - 3)));
});
}