Чистильщик строк для Yii2
Очистка строки от вредоносных и лишних символов с возможностью настройки.
Последовательность действий при настройках по умолчанию:
- [ON] [1] strip_tags($model->$attribute, null)
- [ON] [2] str_replace([‘&’, ‘'’, ‘\’, ‘/’, ‘"’, ‘<’, ‘>’, ‘(’, ‘)’, ‘[’, ’]’], '', $model->$attribute, null)
- [OFF] [3] htmlspecialchars($model->$attribute, ENT_QUOTES, ‘UTF-8’, true)
- [OFF] [4] htmlentities($model->$attribute, ENT_QUOTES, ‘UTF-8’, true)
- [ON] [5] trim($model->$attribute, ’ \t\n\r\0\x0B’)
Вы можете настроить средство проверки, изменив следующие параметры:
- Для [1] - enableStripTags (true/false)
Для [1] - allowableTags (matches the “allowable_tags” parameter)
Для [2] - enableStrReplace (true/false)
- Для [2] - strReplaceSearch (matches the “search” parameter)
- Для [2] - strReplaceReplace (matches the “replace” parameter)
Для [2] - strReplaceCount (matches the “count” parameter)
Для [3] - enableHtmlSpecialChars (true/false)
- Для [3] - htmlSpecialCharsFlags (matches the “flags” parameter)
- Для [3] - htmlSpecialCharsEncoding (matches the “encoding” parameter)
Для [3] - htmlSpecialCharsDoubleEncode (matches the “double_encode” parameter)
Для [4] - enableHtmlEntities (true/false)
- Для [4] - htmlEntitiesFlags (matches the “flags” parameter)
- Для [4] - htmlEntitiesEncoding (matches the “encoding” parameter)
Для [4] - htmlEntitiesDoubleEncode (matches the “double_encode” parameter)
Для [5] - enableTrim (true/false)
- Для [5] - trimCharList (matches the “charlist” parameter)
Примеры
<?php
namespace app\models;
use DanishIgor\Yii2StringCleaner\StringCleaner;
class StringForm extends \yii\base\Model
{
public $string;
public function rules()
{
return [
// По умолчанию.
['string', StringCleaner::class],
// Активация фильтра "htmlentities".
['string', StringCleaner::class, 'enableHtmlEntities' => true],
];
}
}