Polyscope - The agent-first dev environment for Laravel

Laravel Advanced String

mpstenson/laravel-advanced-string image

Laravel Advanced String stats

Downloads
1
Stars
3
Open Issues
0
Forks
1

View on GitHub →

This application provides advanced string functions beyond what Laravel itself provides.

Laravel Advanced String Package

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.

Example

https://github.com/user-attachments/assets/ad4eb551-f383-4ed7-b9db-c44823e637c8

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

Table of Contents

Installation

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,
 
];

Usage

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')

Available Methods

advPassword

Generates a random, secure password.

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

Parameters:

  • $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

Returns:

  • string: Generated password

charWrap

Wraps a string at a given number of characters regardless of words.

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

Parameters:

  • $string (string): The string to wrap
  • $length (int): The number of characters to wrap at (default: 80)

Returns:

  • string: The wrapped string

emailDomain

Extracts the domain part of an email address, including subdomains.

public static function emailDomain(
$string
)

Parameters:

  • $string (string): The string to extract the email domain from.

Returns:

  • string: The email domain from the string

readTime

Calculates the read time of a string.

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

Parameters:

  • $string (string): The text to calculate read time for
  • $wpm (int): Words per minute (default: 200)

Returns:

  • float: Estimated read time in seconds

redactCreditCard

Redacts credit card numbers in a string.

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

Parameters:

  • $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'

Returns:

  • string: The string with credit card numbers redacted

redactSsn

Redacts Social Security Numbers (SSN) in a string.

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

Parameters:

  • $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)

Returns:

  • string: The string with SSNs redacted

splitName

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
)

Parameters:

  • $name (string): The full name to split

Returns:

  • array: An associative array containing 'first', 'middle' (if present), and 'last' name

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

License

The MIT License (MIT). Please see License File for more information.

mpstenson photo

Working in devops and software development @UserScape on @helpspot.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Mpstenson Laravel Advanced String Related Articles

Laravel Advanced String Package image

Laravel Advanced String Package

Read article
The Certification of Competence for Laravel logo

The Certification of Competence for Laravel

A community-driven, proctored assessment across 4 levels designed to validate real-world Laravel knowledge, from Junior to mastery-level Artisan. Official Vue.js, Official Nuxt, Angular, React, JS certifications also available.

The Certification of Competence for Laravel
Honeybadger logo

Honeybadger

Simple developer-focused application monitoring for Laravel. Error tracking, log management, uptime monitoring, status pages, and more!

Honeybadger
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Statamic logo

Statamic

The drop-in ready Laravel CMS you’re been waiting for. Go full-stack or headless, flat file or database – it’s up to you.

Statamic
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
DreamzTech logo

DreamzTech

Hire 6-10+ Yrs. experienced skilled Laravel Developers from DreamzTech. We ensure NDA protected, 100% quality delivery. Contact Us & Discuss Your Need.

DreamzTech