fuitad / string-sequencer
PHP 包,允许您将字符串解析为序列
1.0.1
2016-11-10 14:08 UTC
Requires
- php: >=5.4.0
Requires (Dev)
- phpunit/phpunit: ~4.0
This package is auto-updated.
Last update: 2024-08-27 01:39:42 UTC
README
==============
PHP 包,允许您将字符串解析为序列。
安装
使用 Composer 需求此包
composer require fuitad/string-sequencer
使用方法
在您的 PHP 文件中导入类
use \Fuitad\StringSequencer;
static from() 方法
use \Fuitad\StringSequencer; $str = "http://www.google.com/page[:1:1:5:].html"; $result = StringSequencer::from($str);
将返回。
array:5 [
0 => "http://www.google.com/page1.html"
1 => "http://www.google.com/page2.html"
2 => "http://www.google.com/page3.html"
3 => "http://www.google.com/page4.html"
4 => "http://www.google.com/page5.html"
]
static multi() 方法
$str = "http://www.google.com/page[:1:1:5:]-[:20:5:40:].html"; $result = StringSequencer::multi($str);
将返回。
array:25 [
0 => "http://www.google.com/page1-20.html"
1 => "http://www.google.com/page1-25.html"
2 => "http://www.google.com/page1-30.html"
3 => "http://www.google.com/page1-35.html"
4 => "http://www.google.com/page1-40.html"
5 => "http://www.google.com/page2-20.html"
6 => "http://www.google.com/page2-25.html"
7 => "http://www.google.com/page2-30.html"
8 => "http://www.google.com/page2-35.html"
9 => "http://www.google.com/page2-40.html"
10 => "http://www.google.com/page3-20.html"
11 => "http://www.google.com/page3-25.html"
12 => "http://www.google.com/page3-30.html"
13 => "http://www.google.com/page3-35.html"
14 => "http://www.google.com/page3-40.html"
15 => "http://www.google.com/page4-20.html"
16 => "http://www.google.com/page4-25.html"
17 => "http://www.google.com/page4-30.html"
18 => "http://www.google.com/page4-35.html"
19 => "http://www.google.com/page4-40.html"
20 => "http://www.google.com/page5-20.html"
21 => "http://www.google.com/page5-25.html"
22 => "http://www.google.com/page5-30.html"
23 => "http://www.google.com/page5-35.html"
24 => "http://www.google.com/page5-40.html"
]
格式化
您还可以添加一个与 sprintf 兼容的模式来格式化输出。
$str = "http://www.google.com/page[:1:1:5:%02d:].html"; $result = StringSequencer::from($str);
将返回
array:5 [
0 => "http://www.google.com/page01.html"
1 => "http://www.google.com/page02.html"
2 => "http://www.google.com/page03.html"
3 => "http://www.google.com/page04.html"
4 => "http://www.google.com/page05.html"
]
反向引用
如果您需要在字符串中多次执行相同的迭代(而不是在字符串上执行不同的迭代),您可以使用反向引用。请注意,虽然语法看起来有点像正则表达式的反向引用,但实际上不是。
$str = "http://www.google.com/page[:1:1:5:%02d:]/[\\1].html"; $result = StringSequencer::from($str);
将返回。
array:5 [
0 => "http://www.google.com/page1/1.html"
1 => "http://www.google.com/page2/2.html"
2 => "http://www.google.com/page3/3.html"
3 => "http://www.google.com/page4/4.html"
4 => "http://www.google.com/page5/5.html"
]