kolyunya/string-processor
Composer 安装命令:
composer require kolyunya/string-processor
包简介
PHP string processing library.
关键字:
README 文档
README
Status
Description
This library provides a collection of string processors.
Installation
This library is composer-enabled. The recommended way of using it in your project is to require it via composer.
composer require kolyunya/string-processor
Single processor usage
Basic usage
Each processor implements the ProcessorInterface which contains the process method:
/** * Processes a string and returns a processed version of the original string. * @param string $string A string to process. * @return string A processed version of the original string. */ public function process($string);
Construct a processor and run process($string) on it:
$processor = new Processor(); echo $processor->process($string);
Shorthand usage
You can also use a processor without even instantiating it. Each processor has a static run method.
/** * Processes a string and returns a processed version of the original string. * @param string $string A string to process. * @param object|array $parameters Parameters passed to the processor's constructor. * @return string A processed version of the original string. */ public static function run($string, $parameters = array());
You can pass parameters to the processor's constructor in the $parameters array. You can also pass a single parameter without wrapping it into an array.
echo KebabCaseFormatter::run('snake_case'); // Output: "snake-case" echo Translator::run( 'Лорем ипсум долор сит амет', new RuEnDictionary() ); // Output: "Lorem ipsum dolor sit amet"
Processors combination
Multiprocessor usage
There is a special processor (Multiprocessor) which allows you to combine multiple processors in one. Suppose you want to convert a string to an UPPER-KEBAB case. You can combine two processors using Multiprocessor to solve this problem.
$processor = new Multiprocessor([ new KebabCaseFormatter(), new UpperCaseFormatter(), ]); echo $processor->process('snake_case'); // Output: "SNAKE-CASE" echo $processor->process('CamelCase'); // Output: "CAMEL-CASE"
The UpperCaseFormatter will be applied after the KebabCaseFormatter. Note that either the processors order does not matter in the first example, it actually matters in the second one.
Another common problem example is to generate URL slugs. A string should be purified from punctuation characters, converted to the kebab-case and transliterated. Combine the PunctuationStripper, the KebabCaseFormatter and the Translator using Multiprocessor.
$processor = new Multiprocessor([ new RuEnTranslator(), new AlphabeticalPurifier(), new KebabCaseFormatter(), ]); echo $processor->process('Лорем ипсум долор сит амет'); // Output: "lorem-ipsum-dolor-sit-amet" echo $processor->process('Привет, Мир!'); // Output: "privet-mir"
Processors decoration
Each processor is a decorator. The ProcessorInterface contains the decorate method:
/** * Decorates supplied processor with the current processor. * @param ProcessorInterface $processor Processor to decorate. * @return ProcessorInterface Current processor. */ public function decorate(ProcessorInterface $processor);
That means that the above example can be implemented using processors decoration. The Multiprocessor usage is somewhat more readable though.
$processor = (new RuEnTranslator())->decorate( (new KebabCaseFormatter())->decorate( (new PunctuationStripper()) ) ); echo $processor->process('Лорем ипсум долор сит амет'); // Output: "lorem-ipsum-dolor-sit-amet" echo $processor->process('Привет, Мир!'); // Output: "privet-mir"
Available processors
Currently the following processors are implemented:
- Case switchers - format strings to arbitrary formats.
- CamelCaseFormatter - formats a string to the
CamelCase. - KebabCaseFormatter - formats a string to the
kebab-case. - SnakeCaseFormatter - formats a string to the
snake_case. - UpperCaseFormatter - formats a string to the
UPPER CASE. - LowerCaseFormatter - formats a string to the
lower case.
- CamelCaseFormatter - formats a string to the
- Translators - transliterate strings from one language to another.
- RuEnTranslator - transliterates strings from Russian to English and the other way around.
- Purifiers - purify strings.
- PunctuationStripper - Strips punctuation characters.
- AlphabeticalPurifier - Strips non-alphabetical characters.
- Multiprocessor - combines multiple processors.
kolyunya/string-processor 适用场景与选型建议
kolyunya/string-processor 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.72k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2016 年 03 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「string」 「camel-case」 「kebab-case」 「snake-case」 「string-manipulation」 「string-processing」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 kolyunya/string-processor 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kolyunya/string-processor 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 kolyunya/string-processor 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Convert strings from and to AdaCase, CamelCase, CobolCase, KebabCase, Lowercase, MacroCase, PascalCase, SentenceCase, SnakeCase, TitleCase, TrainCase, and Uppercase
Camel Case Doctrine ORM naming strategy
A set of useful PHP classes.
Convert strings between camelCase, PascalCase, Headline Case, snake_case and more.
Encode integer IDs using a preset or customized symbol set
base62 encoder and decoder also for big numbers with Laravel integration
统计信息
- 总下载量: 5.72k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GNU
- 更新时间: 2016-03-31