tcb13/substringy 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tcb13/substringy

Composer 安装命令:

composer require tcb13/substringy

包简介

A sub string manipulation library with multibyte support that extends Stringy

README 文档

README

A PHP SubString manipulation library with multibyte support that extends Stringy. Offers OO method chaining. Tested and compatible with PHP 5.4+ and HHVM.

This library extends and adds SubString functionality to danielstjules/Stringy you should check it's documentation for methods inherited by SubStringy.

Build Status

Installation

If you're using Composer to manage dependencies, you can include the following in your composer.json file:

{
    "require": {
        "danielstjules/stringy": "^3.1",
        "tcb13/substringy": "^1.0"
    }
}

Then, after running composer update or php composer.phar update, you can load the class using Composer's autoloading:

require 'vendor/autoload.php';

Otherwise, you can simply require the file directly:

require_once 'path/to/SubStringy/src/SubStringy.php';

And in either case, I'd suggest using an alias.

use SubStringy\SubStringy as S;

OO and Chaining

The library offers OO method chaining, as seen below:

use Stringy\Stringy as S;
echo S::create('Fòô     Bàř', 'UTF-8')->collapseWhitespace()->swapCase();  // 'fÒÔ bÀŘ'

Stringy\Stringy has a __toString() method, which returns the current string when the object is used in a string context, ie: (string) S::create('foo') // 'foo'

Use as a Trait

The library also offers the possibility to be used a trait. With this trait you can build your own abstraction of danielstjules/Stringy and combine multiple extensions:

namespace Vendor\YourPackage;

use Stringy\Stringy;
use SubStringy\SubStringyTrait;
use SliceableStringy\SliceableStringyTrait;

class MyStringy extends Stringy
{
    use SubStringyTrait;
    use SliceableStringyTrait;
}

On the example bellow we can use MyStringy to create Stringy objects enhanced with the functionality of both SubStringy and SliceableStringy:

use YourPackage\MyStringy as S;
$sliceableSubstring = S::create('What are your plans today?')->substringAfterFirst('plans ');
echo $sliceableSubstring['4:6'];

Implemented Interfaces

SubStringy\SubStringy implements the IteratorAggregate interface, meaning that foreach can be used with an instance of the class:

$stringy = S::create('Fòô Bàř', 'UTF-8');
foreach ($stringy as $char) {
    echo $char;
}
// 'Fòô Bàř'

It implements the Countable interface, enabling the use of count() to retrieve the number of characters in the string:

$stringy = S::create('Fòô', 'UTF-8');
count($stringy);  // 3

Furthermore, the ArrayAccess interface has been implemented. As a result, isset() can be used to check if a character at a specific index exists. And since Stringy\Stringy is immutable, any call to offsetSet or offsetUnset will throw an exception. offsetGet has been implemented, however, and accepts both positive and negative indexes. Invalid indexes result in an OutOfBoundsException.

$stringy = S::create('Bàř', 'UTF-8');
echo $stringy[2];     // 'ř'
echo $stringy[-2];    // 'à'
isset($stringy[-4]);  // false

$stringy[3];          // OutOfBoundsException
$stringy[2] = 'a';    // Exception

PHP 5.6 Creation

As of PHP 5.6, use function is available for importing functions. SubStringy exposes a namespaced function, SubStringy\create, which emits the same behaviour as SubStringy\SubStringy::create(). If running PHP 5.6, or another runtime that supports the use function syntax, you can take advantage of an even simpler API as seen below:

use function SubStringy\create as s;

// Instead of: S::create('Fòô     Bàř', 'UTF-8')
s('Fòô     Bàř', 'UTF-8')->collapseWhitespace()->swapCase();

Methods

All methods that return a SubStringy object or string do not modify the original. SubStringy objects are immutable.

Since this library extends and adds SubString functionality to danielstjules/Stringy you should check it's documentation (https://github.com/danielstjules/Stringy/blob/master/README.md) for methods that can also be transparently used when working with SubStringy.

Note: If $encoding is not given, it defaults to mb_internal_encoding().

substringAfterFirst

$stringy->substringAfterFirst(string $separator)

Gets the substring after the first occurrence of a separator. If no match is found returns false.

S::create('What are your plans today?')->substringAfterFirst('plans ');

substringAfterLast

$stringy->substringAfterLast(string $separator)

Gets the substring after the last occurrence of a separator. If no match is found returns false.

S::create('This is a String. How cool can a String be after all?')->substringAfterLast('String ');

substringBeforeFirst

$stringy->substringBeforeFirst(string $separator)

Gets the substring before the first occurrence of a separator. If no match is found returns false.

S::create('What are your plans today?')->substringBeforeFirst(' plans');

substringBeforeLast

$stringy->substringBeforeLast(string $separator)

Gets the substring before the last occurrence of a separator. If no match is found returns false.

S::create('What are your plans today? Any plans for tomorrow?')->substringBeforeLast(' plans');

substringBetween

$stringy->substringBetween(string $start, string $end)

Extracts a substring from between two substrings present on the current string.

S::create('What are your plans today?')->substringBetween('your ', ' today');

substringCount

$stringy->substringCount(string $substr)

Count the number of substring occurrences on the current string

S::create('how are you? are you sure you are ok?')->substringCount('are');

Links

The following is a list of libraries that extend Stringy:

Tests

From the project directory, tests can be ran using phpunit

License

Released under the MIT License - see LICENSE.txt for details.

tcb13/substringy 适用场景与选型建议

tcb13/substringy 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 60.92k 次下载、GitHub Stars 达 17, 最近一次更新时间为 2017 年 09 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「helpers」 「utility」 「utf-8」 「utils」 「multibyte」 「string」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 tcb13/substringy 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 tcb13/substringy 我们能提供哪些服务?
定制开发 / 二次开发

基于 tcb13/substringy 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 60.92k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 17
  • 点击次数: 12
  • 依赖项目数: 1
  • 推荐数: 1

GitHub 信息

  • Stars: 17
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-09-10