manuwhat/strip-tags

以自定义和高效的方式从字符串中移除PHP和HTML标签

dev-master 2019-08-28 08:30 UTC

This package is auto-updated.

Last update: 2024-09-06 03:18:38 UTC


README

以自定义和高效的方式从字符串中移除PHP和HTML标签

Build Status Scrutinizer Code Quality Build Status Code Intelligence Status

需求:PHP 7.0+

为什么需要strip-tags包,如果PHP已经有strip_tags函数呢?

如果你

  1. 只想移除具有某些属性的标签

  2. 只想从某些标签中移除某些属性

  3. 只想移除HTML标签,而不移除PHP标签,或者只移除PHP标签而不移除HTML标签

  4. 想要完全移除某些标签,并从其他标签中部分移除一些属性

  5. 想要指定要移除的标签,而不是保留的标签,或者保留的标签而不是要移除的标签。

如何使用它

通过执行以下命令来要求库

composer require manuwhat/strip-tags

然后你可以这样使用它

$data=$data='<!doctype html><html><head>'.$x.$y.'</head><!-- a comment --> <body><?php echo here <br>; ?> <h2 onmousedown="alert(\'keke\');">u</h2><p></p><h2>a</h2></body></html>';
//$data can be a source string or a file
$hstrip=new htmlstrip($data,'remove',array(Htmlstrip::getTags(),true));//strip every thing PHP and HTML TAGS
$hstrip=new htmlstrip($data,'remove',array('<php>,<script>',true));//remove PHP and sript tags
$hstrip=new htmlstrip($data,'replace',array('<php>,<script>',true),array('onemouseover','true'));//remove PHP and sript tags and onemouseover attributes
//tags and attributes can be specified as array instead of string
var_export(htmlspecialchars($hstrip->go()));//execute
//finally you can refine your choices...
var_export(htmlspecialchars($hstrip->go(htmlstrip::TAGS)));//act only on tags
var_export(htmlspecialchars($hstrip->go(htmlstrip::ATTRIBUTES)));//act only on attributes
var_export(htmlspecialchars($hstrip->go(htmlstrip::TAGS_AND_ATTRIBUTES)));//act on tags and  attributes this is the default behavior
var_export(htmlspecialchars($hstrip->go(htmlstrip::TAGS_WITH_ATTRIBUTES)));//act on tags with some attributes 

运行单元测试

composer test