peterujah/php-functions

封装了我项目上经常使用的一些基本可复用的PHP函数。

1.3 2022-10-13 08:26 UTC

This package is auto-updated.

Last update: 2024-09-13 12:58:31 UTC


README

封装了所有基本可复用的PHP函数,这些函数在处理其他项目时非常有用。这个类可能对初学者也很有用。

此类提供了一组通用PHP函数,用于处理多个函数,它可以执行多种类型的字符串值和其他操作的操纵。

目前,它可以

  • 创建一个指定长度的随机值字符串、整数、密钥、盐或字母
  • 将时间戳值转换为社交媒体网站或论坛评论的时间格式
  • 生成并验证一个唯一的标识符字符串
  • 生成统一公共咨询(UPC)
  • 生成欧洲商品编号(EAN)或(EAN13)
  • 基于最小值和最大值生成大整数
  • 检查一个字符串是否是一个有效的电子邮件地址
  • 检查密码复杂性计数,使用通用的安全密码模式
  • 加密密码字符串以创建散列值
  • 解密密码散列并验证是否匹配
  • 计算一组项目的平均评分点
  • 格式化货币值以添加小数位
  • 按百分比折扣值
  • 按百分比增加值
  • 固定或四舍五入数字到特定位数
  • 从数组创建徽章
  • 从数组创建按钮徽章
  • 获取当前用户的IP地址
  • 获取小时列表
  • 格式化用户输入以防止跨站脚本攻击
  • 将字符串字符转换为HTML实体
  • 将文件和文件夹复制到新目录
  • 在浏览器上为文件提供服务以供下载
  • 截断文本
  • 将Base64编码的字符串转换为URL参数
  • 解码Base64编码的URL编码字符串
  • 从字符串中删除不需要的字符
  • 从子域名demo.example.com提取域名
  • 隐藏电子邮件地址
  • 按位置隐藏字符串
  • 删除文件和文件夹
  • 写入新的日志行
  • 保存日志并替换旧内容
  • 查找日志文件

安装

通过Composer安装非常简单

composer require peterujah/php-functions

用法

use \Peterujah\NanoBlock\Functions;
$func = new Functions();

或者扩展这个类并创建自己的新函数,如下所示。

class MyFunction extends \Peterujah\NanoBlock\Functions{
  public function __construct(){
  }
  public function myFunction(){
    //do anything
  }
  public static function myStaticFunction(){
    //do anything
  }
}

然后初始化您的自定义类

$func = new MyFunction();

可用的静态方法

创建一个随机字符串/数字

Functions::Random(10, Functions::INT);

从PHP时间戳创建“过去的时间”,返回字符串

Functions::timeSocial(php_timestamp);

生成一个uuid字符串,返回字符串

Functions::uuid();

验证uuid字符串返回true或false

Functions::is_uuid($uuid);

验证电子邮件地址是否有效返回true或false

Functions::is_email($email);

创建一个密码散列密钥

Functions::Encrypt($password);

验证密码是否与散列密钥匹配

Functions::Decrypt($password, $hash);

检查密码强度

Functions::strongPassword($password, $minLength = 8,$maxLength = 16, $complexity=4);

从子域名提取主要域名

Functions::removeSubdomain("subdomain.example.com"); //returns example.com

计算项目的平均评分点

Functions::calcAverageRating($total_user_reviews, $total_rating_count);

将数字格式化为货币

Functions::Money($number, $fractional);

按百分比从(int、float、double)值中折扣

Functions::Discount(100, 10); // return 90

按百分比增加(int、float、double)值的利息

Functions::Interest(100, 10); //return 110

固定/四舍五入数字

Functions::Fixed(12345.728836, 2);

使用最小值和最大值生成随机数

Functions::BigInteger(12345, 728836);

从数组创建标签/徽章

Functions::badges(
    ["php", "js", "css"], 
    $bootstrap_color_without_prefix, 
    $type_of_badge, 
    $url_prefix_for_links
);

从数组创建按钮标签/徽章

Functions::buttonBadges(
    ["php", "js", "css"], 
    $bootstrap_color_without_prefix, 
    $truncate, 
    $truncate_limit, 
    $selected_button_by_value
);

返回用户IP地址

Functions::IP();

列出时间小时,返回数组

Functions::hoursRange();

根据所需数据类型安全/格式化用户输入

Functions::XSS("Rhd53883773", "int");

格式化:将字符串字符转换为HTML实体

Functions::htmlentities($string, $encode);

生成UPC产品ID

Functions::UPC($prefix = 0, $length = 12);

生成EAN13 ID

Functions::EAN($country = 615, $length = 13);

将文件和文件夹复制到新目录

Functions::copyFiles("path/from/file/", "path/to/file/");

将文件和文件夹复制到新目录

Functions::download(
    "path/to/download/file.zip", //string filepath to download
    "file.zip", // string file name to download
    false // bool delete file after download is complete true or false
);

基于长度截断文本

Functions::truncate(
    $text, //string text to truncate
    $length // int length to display
);

为URL传递进行Base64编码字符串

Functions::base64_url_encode($input);

解码已编码的URL编码字符串

Functions::base64_url_decode($encoded_input);

隐藏电子邮件地址

Functions::maskEmail(
    $email, // string email address
    "*" // character to mask with
);

按位置隐藏字符串

Functions::mask(
    $string, // string to mask
    "#", // character to mask with
    $position  //string position to mask left|right|center"
)

确定密码强度,如果它符合所有基本密码规则,例如

  1. 密码是否满足最小和最大长度要求?
  2. 密码是否包含数字?
  3. 密码是否包含大写字母?
  4. 密码是否包含小写字母?
  5. 密码是否包含特殊字符?
Functions::strongPassword($password, $minLength, $maxLength);

删除文件和文件夹

Functions::remove(
    "path/to/delete/file/", // path to delete files
    false // delete base file once sub files and folders has been deleted
) 

写入新的日志行文件

Functions::writeLog(
    "path/to/logs/", // string path to save logs
    "info.php",  // string log file name, use .php extension to secure log file from accessible in browser
    $data, // mixed log content
    $secure, // bool set true if file is using .php extension security method 
    $serialize, // bool serialize log content
    $replace // bool replace old log content
);

将简写替换参数保存在 Functions::writeLog

Functions::saveLog(
    "path/to/logs/", // string path to save logs
    "info.php",  // string log file name, use .php extension to secure log file from accessible in browser
    $data, // mixed log content
    $secure, // bool set true if file is using .php extension security method 
    $serialize, // bool serialize log content
);

查找日志文件

Functions::findLog(
    "path/to/logs/info.php", //string filepath to log
    $unserialize // bool unserialize content if it was saved in serialize mode
);

从字符串中删除不需要的字符,并在文本区域中显示新行的文本

$func->stripeText(
    $string, // string text to stripe unwanted characters
    $rules, // array rules array("[br/]" =>  "&#13;&#10;","<script>"    => "oops!",)
    $textarea // bool display text inside textarea
);