Tested, community maintained, supercharged Laravel string functions.
Laravel Advanced String is a Laravel package that adds advanced string manipulation methods to the built in Str
class that Laravel provides. You get extended functionality on strings such as advanced password generation, data redaction, and more.
The Laravel Advanced String package by default adds macros to the Str
class so your can access the extended functionality in the same class that your other string methods are found in. You can also disable this functionality the in the package config
and use the AdvStr
class directly.
ssnRedaction-compressed.mp4
Str::redactSsn('My social security number is 222-22-2222'); // My social security number is xxxxxx
OR...
AdvStr::redactSsn('My social security number is 222-22-2222'); // My social security number is xxxxxx
You can install the package via composer:
composer require mpstenson/laravel-advanced-string
You can publish the config file with:
php artisan vendor:publish --tag="laravel-advanced-string-config"
This is the contents of the published config file:
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,
];
The Laravel Advanced String package by default adds macros to the Str
class so your can access the extended functionality immediately
Str::redactSsn('123-45-6789')
Generates a random, secure password.
public static function advPassword(
$length = 32,
$letters = true,
$numbers = true,
$symbols = true,
$spaces = false,
$upperLetters = false,
$lowerLetters = false,
$exclude = []
)
$length
(int): Length of the password (default: 32)$letters
(bool): Include mixed case letters (default: true)$numbers
(bool): Include numbers (default: true)$symbols
(bool): Include symbols (default: true)$spaces
(bool): Include spaces (default: false)$upperLetters
(bool): Include uppercase letters (default: false)$lowerLetters
(bool): Include lowercase letters (default: false)$exclude
(array): Characters to exclude from the password
- string: Generated password
Wraps a string at a given number of characters regardless of words.
public static function charWrap(
$string,
$length = 80
)
$string
(string): The string to wrap$length
(int): The number of characters to wrap at (default: 80)
- string: The wrapped string
Extracts the domain part of an email address, including subdomains.
public static function emailDomain(
$string
)
$string
(string): The string to extract the email domain from.
- string: The email domain from the string
Returns a random phrase with a configurable delimiter.
public static function randomPhrase(
$wordCount,
$separator = '-'
)
$wordCount
(int): The number of words in the phrase.$separator
(string): The separator between words (default: '-').
- string: The generated random phrase.
Returns a random word.
public static function randomWord(
)
- none
- string: A random word
Calculates the read time of a string.
public static function readTime(
$string,
$wpm = 200
)
$string
(string): The text to calculate read time for$wpm
(int): Words per minute (default: 200)
- float: Estimated read time in seconds
Redacts credit card numbers in a string.
public static function redactCreditCard(
$string,
$redacted = '********',
$exclude = []
)
$string
(string): The string containing credit card numbers to redact$redacted
(string): The string to replace credit card numbers with (default: '********')$exclude
(array): An array of credit card types to exclude from redaction. Possible types: 'mastercard','visa','amex','discover','diners','jcb'
- string: The string with credit card numbers redacted
Redacts Social Security Numbers (SSN) in a string.
public static function redactSsn(
$string,
$redacted = '********',
$dashes = true,
$noDashes = true
)
$string
(string): The string containing SSNs to redact$redacted
(string): The string to replace SSNs with (default: '********')$dashes
(bool): Redact SSNs with dashes (default: true)$noDashes
(bool): Redact SSNs without dashes (default: true)
- string: The string with SSNs redacted
Splits a full name into first name, middle name (if present), and last name, removing any prefixes and suffixes. This method can handle both "Firstname Lastname" and "Lastname, Firstname" formats.
public static function splitName(
$name
)
$name
(string): The full name to split
- array: An associative array containing 'first', 'middle' (if present), and 'last' name
composer test
Please see CHANGELOG for more information on what has changed recently.
The MIT License (MIT). Please see License File for more information.