andydune / custom-string-explode

使用自定义用户规则拆分字符串。

v1.3.0 2018-09-19 13:27 UTC

This package is auto-updated.

Last update: 2024-09-29 04:49:01 UTC


README

Build Status Software License Packagist Version Total Downloads

使用用户自定义规则拆分字符串。

安装

使用Composer安装

composer require andydune/custom-string-explode

如果没有全局安装Composer

php composer.phar require andydune/custom-string-explode

或者编辑你的composer.json

"require" : {
     "andydune/custom-string-explode": "^1"
}

并执行命令

php composer.phar update

说明

任何我们想要转换成数组的字符串。字符串可以是任何分隔符的数字集合,也可以是电子邮件集合等。

最好通过具体点来了解它是如何工作的。

规则:数字

use AndyDune\CustomStringExplode\Rule\Numbers;
use AndyDune\CustomStringExplode\StringContainer;

$numbers = new Numbers();
$explode = new StringContainer($numbers);
$results = $explode->explode('123 13-4 00');

// Result is
$result = [123, 13, 4, 00];

规则:电子邮件

use AndyDune\CustomStringExplode\Rule\Email;
use AndyDune\CustomStringExplode\StringContainer;

$rule = new Email();
$explode = new StringContainer($rule);

$results = $explode->explode('Андрей Рыжов,  ;
Andrey Ryzhov,
simple@example.com ,
disposable.style.email.with+symbol@example.com
x@example.com
#!$%&\'*+-/=?^_`{}|~@example.org
"()<>[]:,;@\\\"!#$%&\'-/=?^_`{}| ~.a"@example.org
');

// Result is
$result = [
    'simple@example.com', 
    'disposable.style.email.with+symbol@example.com', 
    'x@example.com', 
    '#!$%&'*+-/=?^_`{}|~@example.org'
];

规则:数字和拉丁字母

我用它从任何文本中提取哈希。

use AndyDune\CustomStringExplode\StringContainer;
use AndyDune\CustomStringExplode\Rule\NumbersAndLatinLetters;

$rule = new NumbersAndLatinLetters();
$explode = new StringContainer($rule);

$results = $explode->explode('adqwdqw123 adasdsa;78
првиетhellow
');

// Result is
$result = [
    'adqwdqw123', 
    'adasdsa', 
    '78', 
    'hellow'
];

规则:分隔符空白字符

它有助于使用任何空白字符分隔符拆分字符串。

use AndyDune\CustomStringExplode\Rule\DelimiterWhitespaceCharacter;
use AndyDune\CustomStringExplode\StringContainer;
$rule = new DelimiterWhitespaceCharacter();
$explode = new StringContainer($rule);

$results = $explode->explode('123 13-4 00');

// Result is
$result = [
    '123', 
    '13-4', 
    '00' 
];

创建自定义规则

你可以根据需要构建自己的字符串拆分规则。所有规则必须实现RuleAbstract接口。

让我们看看代码

namespace AndyDune\CustomStringExplode\Rule;
use AndyDune\CustomStringExplode\StringContainer;

abstract class RuleAbstract
{
    /**
     * @var StringContainer
     */
    protected $container;

    /**
     * @return StringContainer
     */
    public function getContainer()
    {
        return $this->container;
    }

    /**
     * @param StringContainer $container
     */
    public function setContainer($container)
    {
        $this->container = $container;
    }

    public function format($string)
    {
        return trim($string);
    }

    /**
    * @params string $char current char for check
    * @params string $item previously collected char
    * @params array $array array was colected during previous executions of method
    */
    abstract public function check($char, $item, $array);
}

你需要定义方法check。这个方法返回布尔值

  • true - 当前字符可以是字符串的一部分
  • false - 当前字符是分隔符

覆盖方法format以对最终结果数组中的每个项进行最终检查。默认情况下,它会进行修剪。方法可以返回nullfalse,如果项必须从数组中删除。