| 1 |
# php-mb-string |
| 2 |
|
| 3 |
[](https://github.com/jfcherng/php-mb-string/actions) |
| 4 |
[](https://packagist.org/packages/jfcherng/php-mb-string) |
| 5 |
[](https://packagist.org/packages/jfcherng/php-mb-string) |
| 6 |
[](https://github.com/jfcherng/php-mb-string/blob/master/LICENSE) |
| 7 |
[](https://github.com/jfcherng/php-mb-string/stargazers) |
| 8 |
[](https://www.paypal.me/jfcherng/5usd) |
| 9 |
|
| 10 |
A high performance multibyte sting implementation for frequently reading/writing operations. |
| 11 |
|
| 12 |
## Why I Write This Package? |
| 13 |
|
| 14 |
Consider that you have a **LONG** multibyte string and |
| 15 |
you want to do lots of following operations on it. |
| 16 |
|
| 17 |
- Random reading/writing such as `$char = $str[5];` or `$str[5] = '許';`. |
| 18 |
- Replacement such as `str_replace($search, $replace, $str);`. |
| 19 |
- Insertion such as `substr_replace($insert, $str, $position, 0);`. |
| 20 |
- Get substring such as `substr($str, $start, $length);`. |
| 21 |
|
| 22 |
Because strings in PHP are not UTF-8, to do operations above safely, |
| 23 |
you have to either use `mb_*()` functions or calculate the index by yourself. |
| 24 |
Using `mb_*()` functions frequently can be a performance loss because it has |
| 25 |
to re-decode the source string basing on the given encoding every time when you call it. |
| 26 |
The longer the string is, the severer the problem becomes. |
| 27 |
|
| 28 |
Instead, this class internally stores the string in its UTF-32 form, |
| 29 |
which is fixed-width (1 char always occupies 4 bytes) so we are able to |
| 30 |
perform speedy random accesses. With the power of random access, we could |
| 31 |
use `str_*()` functions to do the job internally. |
| 32 |
|
| 33 |
## Installation |
| 34 |
|
| 35 |
```bash |
| 36 |
composer require jfcherng/php-mb-string |
| 37 |
``` |
| 38 |
|
| 39 |
## Example |
| 40 |
|
| 41 |
See [tests/MbStringTest.php](https://github.com/jfcherng/php-mb-string/blob/master/tests/MbStringTest.php). |
| 42 |
|
| 43 |
## Benchmark |
| 44 |
|
| 45 |
See [benchmark/\_results.txt](https://github.com/jfcherng/php-mb-string/blob/master/benchmark/_results.txt). |
| 46 |
|
| 47 |
## What Are You Doing With This Package? |
| 48 |
|
| 49 |
I develop this for a PHP diff package, [jfcherng/php-diff](https://github.com/jfcherng/php-diff). |