mpstenson/laravel-advanced-string

本应用程序提供了比Laravel自身提供的更高级的字符串函数。

1.1.0 2024-08-25 05:59 UTC

This package is auto-updated.

Last update: 2024-09-25 06:11:19 UTC


README

经过测试、社区维护、超级强大的Laravel字符串函数。

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Laravel Advanced String 是一个Laravel包,它向Laravel提供的内置 Str 类添加了高级字符串操作方法。您可以在字符串上获得扩展功能,例如高级密码生成、数据编辑等。

Laravel Advanced String 包默认向 Str 类添加宏,以便您可以在同一类中访问扩展功能,其中包含您其他字符串方法。您还可以在包配置中禁用此功能,并直接使用 AdvStr 类。

示例

ssnRedaction-compressed.mp4
Str::redactSsn('My social security number is 222-22-2222'); // My social security number is xxxxxx

或者...

AdvStr::redactSsn('My social security number is 222-22-2222'); // My social security number is xxxxxx

目录

安装

您可以通过composer安装此包

composer require mpstenson/laravel-advanced-string

您可以使用以下命令发布配置文件

php artisan vendor:publish --tag="laravel-advanced-string-config"

这是已发布配置文件的内容

return [

    /*
    // Macro the AdvStr class to the Illuminate\Support\Str class. You can disable
    // this here if you don't want the AdvStr methods available on the Str class
    */

    'use_str' => true,

];

使用

Laravel Advanced String 包默认向 Str 类添加宏,以便您可以立即访问扩展功能

Str::redactSsn('123-45-6789')

可用方法

advPassword

生成一个随机、安全的密码。

public static function advPassword(
    $length = 32,
    $letters = true,
    $numbers = true,
    $symbols = true,
    $spaces = false,
    $upperLetters = false,
    $lowerLetters = false,
    $exclude = []
)

参数

  • $length (int): 密码长度(默认:32)
  • $letters (bool): 包含大小写字母(默认:true)
  • $numbers (bool): 包含数字(默认:true)
  • $symbols (bool): 包含符号(默认:true)
  • $spaces (bool): 包含空格(默认:false)
  • $upperLetters (bool): 包含大写字母(默认:false)
  • $lowerLetters (bool): 包含小写字母(默认:false)
  • $exclude (array): 从密码中排除的字符

返回

  • string: 生成的密码

charWrap

在不考虑单词的情况下,在给定的字符数处包装字符串。

public static function charWrap(
    $string, 
    $length = 80
)

参数

  • $string (string): 要包装的字符串
  • $length (int): 要包装的字符数(默认:80)

返回

  • string: 包装后的字符串

emailDomain

提取电子邮件地址的域名部分,包括子域名。

public static function emailDomain(
    $string
)

参数

  • $string (string): 从中提取电子邮件域名的字符串

返回

  • string: 从字符串中提取的电子邮件域名

randomPhrase

返回一个具有可配置分隔符的随机短语。

public static function randomPhrase(
    $wordCount,
    $separator = '-'
)

参数

  • $wordCount (int): 短语中的单词数。
  • $separator (string): 单词之间的分隔符(默认:'-').

返回

  • string: 生成的随机短语

randomWord

返回一个随机单词。

public static function randomWord(
)

参数

  • none

返回

  • string: 一个随机单词

readTime

计算字符串的阅读时间。

public static function readTime(
    $string, 
    $wpm = 200
)

参数

  • $string (string): 要计算阅读时间的文本
  • $wpm (int): 每分钟单词数(默认:200)

返回

  • float: 估计的阅读时间(秒)

redactCreditCard

在字符串中编辑信用卡号。

public static function redactCreditCard(
    $string, 
    $redacted = '********', 
    $exclude = []
)

参数

  • $string (string): 包含要编辑的信用卡号的字符串
  • $redacted (string): 用以替换信用卡号的字符串(默认:'********')
  • $exclude (array): 要排除的信用卡类型数组。可能的类型:'mastercard','visa','amex','discover','diners','jcb'

返回

  • string: 编辑信用卡号后的字符串

redactSsn

在字符串中编辑社会安全号码(SSN)。

public static function redactSsn(
    $string, 
    $redacted = '********', 
    $dashes = true, 
    $noDashes = true
)

参数

  • $string (string): 包含要编辑的SSN的字符串
  • $redacted (string): 用以替换SSN的字符串(默认:'********')
  • $dashes (布尔值): 用破折号隐藏SSN(默认:true)
  • $noDashes (布尔值): 不用破折号隐藏SSN(默认:true)

返回

  • 字符串:已隐藏SSN的字符串

splitName

将全名分割为名、中名(如果有)和姓,移除任何前缀和后缀。此方法可以处理“FirstName LastName”和“LastName, FirstName”格式。

public static function splitName(
    $name
)

参数

  • $name (字符串): 要分割的全名

返回

  • 数组:包含 'first'、'middle'(如果有)和 'last' 名字的关联数组

测试

composer test

变更日志

有关最近更改的更多信息,请参阅变更日志

致谢

许可

MIT许可证(MIT)。有关更多信息,请参阅许可证文件