bayfrontmedia/php-sanitize

用于清理、过滤和转换数据的简单类。

v2.0.0 2023-01-26 16:58 UTC

This package is auto-updated.

Last update: 2024-08-26 20:21:06 UTC


README

用于清理、过滤和转换数据的简单类。

许可证

此项目是开源的,可在MIT许可证下使用。

作者

Bayfront Media

需求

  • PHP ^8.0

安装

composer require bayfrontmedia/php-sanitize

用法

转换

描述

确保给定的变量将被返回为特定类型。默认为 "字符串"。

参数

  • $var (mixed)
  • $type = 'string' (string)

有效的 $type 值有

  • int
  • float
  • bool
  • object
  • array
  • string

返回

  • (mixed)

示例

use Bayfront\Sanitize\Sanitize;

$input = '1.42';

echo Sanitize::cast($input, 'float');

电子邮件

描述

过滤字符串以获取有效的电子邮件字符。

参数

  • $email (string)

返回

  • (string)

示例

use Bayfront\Sanitize\Sanitize;

echo Sanitize::email('email@example.com');

URL

描述

过滤字符串以获取有效的URL字符。

参数

  • $url (string)

返回

  • (string)

示例

use Bayfront\Sanitize\Sanitize;

echo Sanitize::url('https://www.example.com);

路径

描述

过滤字符串以获取有效的路径语法,可选尾部斜杠。

此方法删除任何空白、空格和前导斜杠。它还将反向和多个斜杠转换为单个正斜杠。

参数

  • $path (string)
  • $trailing = true (bool): 需要尾部斜杠

返回

  • (string)

示例

use Bayfront\Sanitize\Sanitize;

$path = '/some/ bad//path';

echo Sanitize::path($path);

转义

描述

转义字符串和数组。其他数据类型返回其原始值。

注意:在转义整个数组时请谨慎,因为字符串通常只在输出到HTML时才应进行转义。

参见:https://php.ac.cn/manual/en/mbstring.supported-encodings.php

参数

  • $value (mixed)
  • $encoding = 'UTF-8' (string)

返回

  • (mixed)

示例

use Bayfront\Sanitize\Sanitize;

$html = '<a href="#">Hyperlink</a>';

echo Sanitize::escape($html);