hota1024 / php-string
PHP 字符串包装类和方法
dev-master
2018-01-31 07:51 UTC
Requires (Dev)
- phpunit/phpunit: 7.1.*
This package is not auto-updated.
Last update: 2024-10-02 03:44:23 UTC
README
PHP 字符串包装类和方法。
安装
使用 composer
输入以下命令。
composer require hota1024/php-string
不能使用 composer 吗?
OK。
- 下载此仓库。
- 解压 PHPString-master.zip。
- 您可以使用
PHPString-master/src/hota1024/String.php
。
如何使用它?
composer
require_once "path/to/vendor/autoload.php"; $str = new hota1024\Str('Hello world!'); echo $str; # Hello world!
不使用 composer
include "path/to/String.php"; $str = new hota1024\Str('Hello world!'); echo $str; # Hello world!
教程
步骤1「创建 String 对象」
$string = new hota1024\Str('string value'); # Create string object.
麻烦的编码 hota1024\Str?
您可以使用此代码。
use \hota1024\Str;
use \hota1024\Str; $string = new hota1024\Str('string value');
步骤2「输出字符串值」
通常使用 echo
。
$helloworld = new Str('Hello world!'); echo $helloworld; # Hello world!
如果您希望面向对象编程代码?
您可以使用此方法。
$helloworld = new Str('Hello world!'); $helloworld->output(); # Hello world!
步骤3「字符串到数组」
$helloworld = new Str('Hello world!'); var_dump($helloworld->asArray()); # Array ( [0] => H [1] => e [2] => l [3] => l [4] => o [5] => [6] => w [7] => o [8] => r [9] => l [10] => d [11] => ! )
步骤4「反转字符串」
您可以创建反转字符串。
$apple = new Str('Apple'); $apple->reverse(); echo $apple; # elppA
您需要反转的值吗?
$apple = new Str('Apple'); $rev = $apple->reversed(); echo $rev; # elppA
步骤4.1「编写紧凑代码」
$apple = new Str('Apple'); $apple->reverse(); echo $apple; # elppA
↓
$apple = new Str('Apple'); $apple->reverse()->output(); # elppA