oaksoftwaredev/yii2-sanitizer 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

oaksoftwaredev/yii2-sanitizer

Composer 安装命令:

composer require oaksoftwaredev/yii2-sanitizer

包简介

Yii2 Sanitizer is an easy way to sanitize or filter your inputs.

README 文档

README

Yii2 Sanitizer is an easy way to sanitize or filter your inputs for application that uses Yii2 framework.

PHP 8.x

2020-10-14 Forked to update for PHP 8.x

Installation

The preferred way to install this extension is through composer.

Either run

composer require oaksoftwaredev/yii2-sanitizer "^1.0.0"

or add

"oaksoftwaredev/yii2-sanitizer": "^1.0.0"

to the require section of your composer.json file.

Release Changes

NOTE: Refer the CHANGE LOG for details on changes to various releases.

Usage

Add this to the component section of your main config.

'components' => [
    ...
    'sanitizer' => [           
        'class' => 'oaksoftwaredev\Yii2Sanitizer\Sanitize',                   
    ],
    ...
],

Examples

Filters an array on inputs

// data to be filtered
$data = [
    'name' => ' Farhan"',
    'address' => '<p>No 1, Residence ABC</p>',
    'postcode' => '81221A',
    'points' => '-152.1711B',
    'status' => 'C1',
];

// create a filters corresponding to the input data array
$filters = [
    'name' => ['trim', 'escape'],
    'address' => ['trim', 'escape', 'cast:string'],
    'postcode' => ['digit'],
    'points' => ['digit'],
    'status' => ['cast:int'],
];

// call sanitize function to filter an array of inputs
$filtered_data = Yii::$app->sanitizer->sanitize($data, $filters);
var_dump($filtered_data);

Results in:

[
    'name' => 'Farhan\\\"'
    'address' => 'No 1, Residence ABC'
    'postcode' => 81221
    'points' => -152.1711
    'status' => '1'
]

Usage is fairly simple. Your filter array needs to match the data array with the filters as the array value. You can combine the filters and it will be executed in order from left to right.

Filter single value

// call filter function to filter a single value. You can add filter type as the second argument.
$filtered_data = Yii::$app->sanitizer->filter('<p>No 1, Residence ABC</p>', ['trim', 'cast:string']);
echo $filtered_data;

Results in:

No 1, Residence ABC

The first argument would be the value that needs filtering. The second argument would be the filters that needs to be applied.

Available Filters

Here are the filters that you can use:

Filter Description
cast Cast given value into given type. Options are int, float, string.
digit Will removes non-digit from given value.
encode Escaping unwanted tags and only output plain HTML using Yii2 HTML:encode() function.
escape Quote string with slashes using filter_var (FILTER_SANITIZE_ADD_SLASHES) function.
float Removes unwanted characters and remain a float number only. Similar to cast:float.
int Removes unwanted characters and remain a integer number only. Similar to case:int.
purify Purify HTML content using Yii2 HtmlPurifier::process() function. Note that HtmlPurifier processing is quite heavy so use with caution.
strip_tags Removes HTML tags from given string value.
trim Trims a string and remove white spaces.

License

Yii2 Sanitizer is released under the MIT license

oaksoftwaredev/yii2-sanitizer 适用场景与选型建议

oaksoftwaredev/yii2-sanitizer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 754 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 10 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 oaksoftwaredev/yii2-sanitizer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 oaksoftwaredev/yii2-sanitizer 我们能提供哪些服务?
定制开发 / 二次开发

基于 oaksoftwaredev/yii2-sanitizer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 754
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 9
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-10-14