markenwerk/string-builder

此包已被废弃,不再维护。作者建议使用 chroma-x/string-builder 包。

一个提供不同字符串方法的PHP字符串构建器库。

1.0.12 2021-01-18 15:43 UTC

This package is auto-updated.

Last update: 2021-01-18 15:59:03 UTC


README

Build Status Test Coverage Dependency Status SensioLabs Insight Code Climate Latest Stable Version Total Downloads License

一个提供不同字符串方法的PHP基本字符串构建器库。

安装

{
   	"require": {
        "chroma-x/string-builder": "~1.0"
    }
}

使用方法

自动加载和命名空间

require_once('path/to/vendor/autoload.php');

构建和修改字符串

use ChromaX\StringBuilder\StringBuilder;

$builder = new StringBuilder('rolod muspi meroL');
$builder
	->reverse()
	->append(' sit amet, consetetur')
	->append(12)
	->append(false)
	->prepend('b')
	->append(true)
	->insert(1, 'qäs')
	->replace(6, 2, 'wertz')
	->setCharAt(4, '2')
	->delete(0, 2)
	->delete(40)
	->deleteCharAt(3);

$result = $builder->build();
fwrite(STDOUT, ' 1. Built string                                          ' . $result . PHP_EOL);

$result = $builder->buildSubstring(5, 2);
fwrite(STDOUT, ' 2. Built substring                                       ' . $result . PHP_EOL);

$result = $builder->buildSubstring(5);
fwrite(STDOUT, ' 3. Built substring                                       ' . $result . PHP_EOL);

$result = $builder->charAt(5);
fwrite(STDOUT, ' 4. Character at position 5                               ' . $result . PHP_EOL);

$result = $builder->firstChar();
fwrite(STDOUT, ' 5. First character                                       ' . $result . PHP_EOL);

$result = $builder->lastChar();
fwrite(STDOUT, ' 6. Last character                                        ' . $result . PHP_EOL);

将输出以下内容

 1. Built string                                          ä2wertzem ipsum dolor sit amet, conset
 2. Built substring                                       rt
 3. Built substring                                       rtzem ipsum dolor sit amet, conset
 4. Character at position 5                               r
 5. First character                                       ä
 6. Last character                                        t

获取字符串属性

$result = $builder->length();
fwrite(STDOUT, ' 7. String length                                         ' . $result . PHP_EOL);

$result = $builder->size();
fwrite(STDOUT, ' 8. Number of bytes                                       ' . $result . PHP_EOL);

$result = $builder->indexOf('e');
fwrite(STDOUT, ' 9. First occurence of "e"                                ' . $result . PHP_EOL);

$result = $builder->indexOf('e', 5);
fwrite(STDOUT, '10. First occurence of "e" after position 5               ' . $result . PHP_EOL);

$result = $builder->lastIndexOf('e');
fwrite(STDOUT, '11. Last occurence of "e"                                 ' . $result . PHP_EOL);

$result = $builder->lastIndexOf('e', 5);
fwrite(STDOUT, '12. Last occurence of "e" before the 5th last character   ' . $result . PHP_EOL);

$result = $builder->contains('ipsum');
fwrite(STDOUT, '13. Whether the string contains "ipsum"                   ' . $result . PHP_EOL);

将输出以下内容

 7. String length                                         38
 8. Number of bytes                                       39
 9. First occurence of "e"                                4
10. First occurence of "e" after position 5               8
11. Last occurence of "e"                                 37
12. Last occurence of "e" before the 5th last character   29
13. Whether the string contains "ipsum"                   <TRUE>

异常处理

所有方法在配置错误时都会抛出 \InvalidArgumentException 异常,除了 indexOflastIndexOf,它们在给定的子字符串不在构建的字符串中时返回 null

use ChromaX\StringBuilder\StringBuilder;

try {
	$builder = new StringBuilder();

	$result = $builder->indexOf('a');
	fwrite(STDOUT, '1. Result                 ' . $result . PHP_EOL);

	$result = $builder->lastIndexOf('a');
	fwrite(STDOUT, '2. Result                 ' . $result . PHP_EOL);

	$result = $builder->charAt(10);
	fwrite(STDOUT, '3. Result                 ' . $result . PHP_EOL);

} catch (\InvalidArgumentException $exception) {
	fwrite(STDERR, 'Exception with message    ' . $exception->getMessage() . PHP_EOL);
}

将输出以下内容

1. Result                 <NULL>
2. Result                 <NULL>
Exception with message    Position invalid

贡献

对项目的贡献总是非常受欢迎。
但:请遵循在 CONTRIBUTING.md 文档中记录的贡献指南。

许可

PHP String Builder 在 MIT 许可下。