dmeroff/string

PHP 5.3 字符串操作库

v1.0 2013-07-15 14:00 UTC

This package is not auto-updated.

Last update: 2024-09-23 14:18:45 UTC


README

String 类

String 类可以将字符串转换为 PHP 对象。它实现了流畅的接口,提高了我们对字符串的处理方式,并通过提供常用函数扩展了功能。

想象一下,你需要将 "hello" 替换为 "goodbye",并使第一个字母大写。在经典的 PHP 中,它可能看起来像这样

$string = 'hello, world';
$string = str_replace('hello', 'goodbye', $string);
$string = ucfirst($string);
echo $string; // Goodbye, world

使用 String 类会使事情变得更简单

$string = 'hello, world';
$string = new String\String($string);
echo $string->replace('hello', 'goodbye')->sentencecase(); // Goodbye, world

或者

$string = 'hello, world';
echo String\String::make($string)->replace('hello', 'goodbye')->sentencecase(); // Goodbye, world

LetterPairSimilarity 类

此类可以使用 "Strike a match" 算法 来比较字符串。

$similarity = String\LetterPairSimilarity::compare('Healed', 'Healthy');
echo round($similarity, 2); // 0.55

要求

  • PHP 5.3 及更高版本
  • mbstring 扩展