jonasrudolph/php-component-stringutility

此包已 弃用 且不再维护。未建议替代包。

1.2.1 2017-03-02 07:44 UTC

This package is not auto-updated.

Last update: 2023-11-11 14:06:08 UTC


README

Build Status Dependency Status
BCH compliance Codacy Badge

php-component-stringutility

  • 字符串测试函数
use JonasRudolph\PHPComponents\StringUtility\Base\StringUtilityInterface;
use JonasRudolph\PHPComponents\StringUtility\Implementation\StringUtility;

/** @var StringUtilityInterface */
$stringUtility = new StringUtility();

$stringUtility->contains('mySecr3t', 'cr3') === true

$stringUtility->startsWith('https://my-domain.com', 'https') === true

$stringUtility->endsWith('www.my-domain.com/img/logo.svg', '.svg') === true;

$stringUtility->removePrefix('http://www.my-domain.com', 'http://') === 'www.my-domain.com'

$stringUtility->removeSuffix('my-domain.com', '.com') === 'my-domain'

$stringUtility->removeWhitespace("Any String\tWith\nWhitespace\r.\0") === 'AnyStringWithWitespace.'

$stringUtility->substringAfter('www.my-domain.com/index.php', '.com/') === 'index.php'

$stringUtility->substringBefore('www.my-domain.com/user?id=1&token=xyz', '?') === 'www.my-domain.com/user'

$stringUtility->substringBetween('there is no foo without a bar', 'no ', ' without') === 'foo'

$stringUtility->split('user@my-domain.com:secret', ':') === ['user@my-domain.com', 'secret']

所有函数使用一致的规则

  • 当函数在字符串中搜索针(needle)时,如果字符串中针出现多次 - 则,函数将使用字符串中针的第一个出现进行后续计算
    这意味着

    $stringUtility->substringAfter('abefbg', 'b') === 'efbg'
    $stringUtility->substringBefore('abefbg', 'b') === 'a'
    $stringUtility->split('user@my-domain.com:secret', ':') === ['user@my-domain.com', 'secret']

    一个例外是 substringBetween 函数,它将在第一个 $startAfter-string 出现后搜索第一个 $untilBefore-string

    $stringUtility->substringBetween('abefbg', 'b', 'b') === 'ef'
    $stringUtility->substringBetween('abefbg', 'e', 'b') === 'f'
    $stringUtility->substringBetween('abcdefg', 'c', 'b') === 'defg'
  • 字符串中的每个字符都被空字符串 ('') 无限包围
    这意味着

    $stringUtility->contains($myString, '') === true
    $stringUtility->startsWith($myString, '') === true
    $stringUtility->endsWith($myString, '') === true;
    $stringUtility->substringAfter($myString, '') === $myString
    $stringUtility->substringBefore($myString, '') === ''
    $stringUtility->substringBetween('abcd', '', 'b') === 'a'
    $stringUtility->substringBetween($myString, $x, '') === ''
    $stringUtility->split($myString, '') === ['', $myString]

贡献者