mortimer333 / content
dev-master
2022-10-11 19:35 UTC
Requires
- php: >=8.1
This package is auto-updated.
Last update: 2024-09-11 23:53:57 UTC
README
允许对多字节字符串进行适当的迭代和操作
注意:当前仅支持UTF-8
安装
composer require mortimer333/content
为什么?
如果您要遍历使用UTF-8编码的文件中的字符串
$string = "óźćżó→ę"; for ($i=0; $i < strlen($string); $i++) { echo $string[$i] . ', '; }
您的输出将是
�, �, �, �, �, �, �, �, �, �, �, �, �, �, �,
这是因为UTF-8将大多数符号存储在多个字节中,而PHP一次访问一个字节。要正确遍历此类字符串,您必须手动将字符串分割成合适的字母。或者使用这个库
$content = new Content\Utf8("óźćżó→ę"); for ($i=0; $i < $content->getLength(); $i++) { echo $content->getLetter($i) . ', '; }
输出
ó, ź, ć, ż, ó, →, ę,
性能
创建新类成本高昂,因此您可以只创建一个Content
实例,并向其中添加新的版本/新字符串
$content = new Content\Utf8("óźćżó→ę"); echo "Version 1: " . $content; // óźćżó→ę $content->cutAndAddContent("ÐÑÒÓÔ"); echo "Version 2: " . $content; // ÐÑÒÓÔ // If you don't specify constent keeps his older version in memory if it might be needed later // (to skip the expensive operation of cutting the same string into chunks again) $content->removeContent(); echo "Version 1: " . $content; // óźćżó→ę
这个功能确实缺乏一些灵活性(例如为版本分配ID或能够在不删除当前版本的情况下检索版本),但将在未来添加。
操作
转换为字符串
类实现了_toString
方法,因此任何实例都可以直接转换为字符串
$string = new Content\Utf8('Foo'); echo (string) $string; // Foo
subStr
您也可以使用subStr
或iSubStr
获取内容的一部分
$string = new Content\Utf8('Foo and Bar'); // By index and length echo $string->subStr(0, 3); // Foo // From index to index echo $string->iSubStr(8, 10); // Bar
cutToContent
类似于subStr
,cutToContent
返回字符串的一部分,但以新Content的形式返回
$string = new Content\Utf8('Foo and Bar'); // By index and length echo $string->cutToContent(0, 3); // Utf8(Foo) // From index to index echo $string->iCutToContent(8, 10); // Utf8(Bar)
cutToArray
类似于subStr
和cutToContent
,但以数组形式返回字符串的块
$string = new Content\Utf8('Foo and Bar'); // By index and length var_dump($string->cutToArray(0, 3)); // Array("F", "o", "o") // From index to index var_dump($string->iCutToArray(8, 10)); // Array("B", "a", "r")
trim
如果需要,您还可以修剪内容(它将返回修剪后的版本,而不是替换现有版本)
$string = new Content\Utf8('==Foo and Bar=='); echo (string) $string->trim("="); // Foo and Bar;
splice
如果您想从当前版本中删除字符串的一部分或替换它/注入新字符串,可以使用splice
方法
$string = new Content\Utf8('Foo #error Bar'); $string->splice(4, 6, 'and'); // Foo and Bar echo (string) $string->iSplice(4, 6, 'or'); // Foo or Bar
find
如果您需要查找某个针的索引,可以使用find
方法
$string = new Content\Utf8('Foo findme Bar'); echo "`findme` starts at index " . $string->find('findme'); // `findme` starts at index 9
reverse
如果您需要反转字符串,可以使用reverse
方法
$string = new Content\Utf8('ź = ć'); echo (string) $string->reverse(); // ć = ź
静态方法
Content\[encoding]::get
每种编码(目前只有UTF-8)都必须实现一个名为get
的静态方法,该方法接收字符串并返回最接近的字母
$string = "źćó"; $start = $nextLetter = 0; $letter = Content\Utf8::get($string, $start, $nextLetter); echo $letter; // "ź"; // Notice that $nextLetter got updated with position of the end byte // of the letter that was found, so if we use it as start we can get next letter $letter = Content\Utf8::get($string, $nextLetter, $nextLetter); echo $letter; // "ć";
Content\[encoding]::isWhitespace
此方法检查传入的字符串是否仅包含空白字符
Content\Utf8::isWhitespace("\n\t\r"); // true Content\Utf8::isWhitespace(" notWhitespace "); // false Content\Utf8::isWhitespace(""); // false // It checks all whitespaces defined in unicode // so it's a better then `trim` or `ctype_space` in terms of validating string $string = "\u{2003}\u{2000}\u{2009}"; Content\Utf8::isWhitespace($string); // true \mb_strlen(trim($string)); // 3 ctype_space($string); // false