定制 victoryoalli/laravel-string-macros 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

victoryoalli/laravel-string-macros

Composer 安装命令:

composer require victoryoalli/laravel-string-macros

包简介

A set of useful Laravel string macros

README 文档

README

GitHub release (latest by date) Packagist Downloads

A collection of useful string macros for Laravel's Str and Stringable classes.

Requirements

Version PHP Laravel
2.1+ ^8.2 10.x, 11.x, 12.x
2.0 ^8.1 10.x, 11.x
1.x ^8.0 8.x, 9.x, 10.x

Installation

composer require victoryoalli/laravel-string-macros

Available Macros

All macros are available both as static methods on Str and as fluent methods on Stringable.

initials

Gets the initials of the words you provide. Defaults to 2 initials.

use Illuminate\Support\Str;

// Static usage
Str::initials('Victor Yoalli Dominguez');
// "VY"

Str::initials('Victor Yoalli Dominguez', 3);
// "VYD"

// Fluent usage
Str::of('Victor Yoalli Dominguez')->initials()->toString();
// "VY"

Str::of('Victor Yoalli Dominguez')->initials(3)->upper()->toString();
// "VYD"

Note: Words are split by spaces, commas, underscores, and hyphens. For example, Jean-Pierre is treated as two words.

interpolate

Replaces ? placeholders with the values you provide.

use Illuminate\Support\Str;

// Static usage - multiple arguments
Str::interpolate('Roses are ? Violets are ?', 'RED', 'BLUE');
// "Roses are RED Violets are BLUE"

// Static usage - array
Str::interpolate('Roses are ? Violets are ?', ['RED', 'BLUE']);
// "Roses are RED Violets are BLUE"

// Static usage - spread operator
Str::interpolate('Roses are ? Violets are ?', ...['RED', 'BLUE']);
// "Roses are RED Violets are BLUE"

// Fluent usage
Str::of('Hello ? and ?')->interpolate(['World', 'Universe'])->toString();
// "Hello World and Universe"

readingMinutes

Calculates how many minutes it takes to read the text. Accepts HTML (tags are stripped for accurate calculation). Default reading speed is 200 words per minute.

use Illuminate\Support\Str;

// Static usage
Str::readingMinutes('Your long article text here...');
// 1

// With custom words per minute
Str::readingMinutes($longText, 150);
// 3

// Fluent usage
Str::of('<p>HTML content</p>')->readingMinutes()->toString();
// "1"

linesCount

Counts the number of lines in a string (splits by \n or \r).

use Illuminate\Support\Str;

// Static usage
Str::linesCount("Line 1\nLine 2\nLine 3");
// 3

// Fluent usage
Str::of("Hello\nWorld")->linesCount()->toString();
// "2"

Fluent Chaining

All macros can be chained with other Stringable methods:

use Illuminate\Support\Str;

$result = Str::of('? ? ?')
    ->interpolate(['Victor', 'Yoalli', 'Dominguez'])
    ->initials(3)
    ->upper()
    ->toString();
// "VYD"

Upgrading from v1.x to v2.x

Breaking Changes

Removed Macros

The following macros have been removed because they are now available natively in Laravel 10+:

Removed Macro Use Instead Available Since
Str::human($value) Str::headline($value) Laravel 9.x
Str::matches($pattern, $value) Str::isMatch($pattern, $value) Laravel 10.x

Migration Examples

Before (v1.x):

// human() - converts to human readable format
Str::human('hello_world');  // "hello world"
Str::of('hello_world')->human();

// matches() - check if string matches regex
Str::matches('/^hello/', 'hello world');  // true
Str::of('hello world')->matches('/^hello/');

After (v2.x) - Use Laravel native methods:

// headline() - converts to title case with spaces
Str::headline('hello_world');  // "Hello World"
Str::of('hello_world')->headline();

// isMatch() - check if string matches regex
Str::isMatch('/^hello/', 'hello world');  // true
Str::of('hello world')->isMatch('/^hello/');

Note: Str::headline() capitalizes each word, while the old Str::human() did not. If you need lowercase output, chain with ->lower():

Str::of('hello_world')->headline()->lower()->toString();
// "hello world"

PHP and Laravel Version Requirements

Requirement v1.x v2.0 v2.1+
PHP ^8.0 ^8.1 ^8.2
Laravel 8.x, 9.x, 10.x 10.x, 11.x 10.x, 11.x, 12.x

Upgrade Steps

  1. Update your composer.json or run:

    composer require victoryoalli/laravel-string-macros:^2.0
  2. Search and replace removed macros in your codebase:

    # Find usages of removed macros
    grep -r "Str::human\|->human(" app/
    grep -r "Str::matches\|->matches(" app/
  3. Replace with native Laravel methods:

    • Str::human()Str::headline()
    • Str::matches()Str::isMatch()
    • ->human()->headline()
    • ->matches()->isMatch()
  4. Run your tests to ensure everything works correctly.

Testing

composer test

Changelog

Please see CHANGELOG for more information about recent changes.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email victoryoalli@gmail.com instead of using the issue tracker.

Credits

License

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

victoryoalli/laravel-string-macros 适用场景与选型建议

victoryoalli/laravel-string-macros 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.55k 次下载、GitHub Stars 达 8, 最近一次更新时间为 2020 年 08 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「victoryoalli」 「laravel-string-macros」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 victoryoalli/laravel-string-macros 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 victoryoalli/laravel-string-macros 我们能提供哪些服务?
定制开发 / 二次开发

基于 victoryoalli/laravel-string-macros 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

与 victoryoalli/laravel-string-macros 相关的其它包

同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:

统计信息

  • 总下载量: 3.55k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 8
  • 点击次数: 13
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 8
  • Watchers: 0
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-08-15