PHP code example of yvan / purifier
1. Go to this page and download the library: Download yvan/purifier library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
yvan / purifier example snippets
sudo composer
$params = [
'one' => '<h1>one</h1>',
'two' => '<h1><script>alert(123);</script>two</h1>',
];
$purifier = new \Yvan\Purifier\Purifier();
$result = $purifier->remove($params);
print_r($result);
Array
(
[one] => <p>one</p>
[two] => <p>two</p>
)
return [
'encoding' => 'utf-8', //编码
'finalize' => true, //固定
'cachePath' => '/cache/purifier', //缓存路径
'cacheFileMode' => 0755,
'settings' => [
'default' => [ //默认配置
'HTML.Trusted' => true, //信任html元素,如form
'AutoFormat.RemoveEmpty' => true, //移除空
],
'help' => [ //自定义配置
'HTML.AllowedElements' => 'p,span,img,a,strong,em,hr,br', //在受安全支持元素的范围下定义信任元素
'CSS.AllowedProperties' => 'background,background-attachment,background-clip,background-color,background-image,background-origin,background-position,background-repeat,background-size,border,border-box,border-bottom,border-bottom-color,border-bottom-left-radius,border-bottom-right-radius,border-bottom-style,border-bottom-width,border-collapse,border-color,border-image,border-image-outset,border-image-repeat,border-image-slice,border-image-source,border-image-width,border-left,border-left-color,border-left-style,border-left-width,border-radius,border-right,border-right-color,border-right-style,border-right-width,border-spacing,border-style,border-top,border-top-color,border-top-left-radius,border-top-right-radius,border-top-style,border-top-width,border-width,box-decoration-break,box-shadow,box-sizing,box-snap,box-suppress,break-after,break-before,break-inside,caption-side,clear,color,color-interpolation-filters,display,display-inside,display-list,display-outside,font,font-family,font-feature-settings,font-kerning,font-language-override,font-size,font-size-adjust,font-stretch,font-style,font-synthesis,font-variant,font-weight,height,letter-spacing,lighting-color,list-style,list-style-image,list-style-position,list-style-type,margin,margin-bottom,margin-left,margin-right,margin-top,max-height,max-lines,max-width,min-height,min-width,padding,padding-bottom,padding-left,padding-right,padding-top,text-align,text-align-last,text-decoration,text-decoration-color,text-decoration-line,text-decoration-skip,text-decoration-style,text-emphasis,text-height,text-indent,text-justify,text-orientation,text-overflow,text-shadow,text-space-collapse,text-transform,text-underline-position,text-wrap,width,word-break,word-spacing,word-wrap,z-index,right,left,top,bottom,cursor,line-break,size',
'AutoFormat.RemoveEmpty' => true,
],
],
'custom_definition' => [//自定义不受支持的元素和属性
'id' => 'definition_cache_id', //缓存ID、固定
'rev' => 1,//自增值,固定
'debug' => false,//false不打开调试,即不缓存
'elements' => [//自定义元素
['section', 'Block', 'Flow', 'Common'],
['nav', 'Block', 'Flow', 'Common'],
['article', 'Block', 'Flow', 'Common'],
['aside', 'Block', 'Flow', 'Common'],
['header', 'Block', 'Flow', 'Common'],
['footer', 'Block', 'Flow', 'Common'],
['address', 'Block', 'Flow', 'Common'],
],
'attributes' => [//自定义属性
['table', 'height', 'Text'],
['td', 'border', 'Text'],
['th', 'border', 'Text'],
['tr', 'width', 'Text'],
['tr', 'height', 'Text'],
['tr', 'border', 'Text'],
],
],
];
//受信任的的元素
'HTML.AllowedElements' => 'a,abbr,address,b,bdo,big,blockquote,br,caption,cite,code,col,colgroup,dd,del,div,dl,dt,em,font,h1,h2,h3,h4,h5,h6,hr,i,img,ins,li,ol,p,pre,s,small,span,sub,sup,strong,table,tbody,td,tfoot,th,thead,tr,tt,u,ul'
//受信任的的属性
'CSS.AllowedProperties' => 'background,background-attachment,background-color,background-image,background-position,background-repeat,border,border-bottom,border-bottom-color,border-bottom-style,border-bottom-width,border-collapse,border-color,border-left,border-left-color,border-left-style,border-left-width,border-right,border-right-color,border-right-style,border-right-width,border-spacing,border-style,border-top,border-top-color,border-top-style,border-top-width,border-width,caption-side,clear,color,font,font-family,font-size,font-style,font-variant,font-weight,height,letter-spacing,list-style,list-style-image,list-style-position,list-style-type,margin,margin-bottom,margin-left,margin-right,margin-top,padding,padding-bottom,padding-left,padding-right,padding-top,text-align,text-decoration,text-indent,text-transform,width,word-spacing'