stk2k/string

This package is abandoned and no longer maintained. The author suggests using the https://github.com/stk2k/xstring package instead.

Basic string library

0.3.5 2021-06-25 04:10 UTC

This package is auto-updated.

Last update: 2021-07-03 01:56:20 UTC


README

Latest Version on Packagist Software License Build Status Coverage Status Code Climate Total Downloads

Description

Basic string library

Feature

  • Supports ascii string(aString) and multibyte string(mbString)
  • Provide facade interface(StringUtil)

Usage

StringUtil

use stk2k\string\StringUtil;

// Length
echo StringUtil::length('Hello');    // 5
echo StringUtil::length('你好', true);    // 2

// Join
echo StringUtil::join(',', [1,2,3]);    // 1,2,3

// Index of
echo StringUtil::indexOf('Hello', 'e');    // 1

// Contains
echo StringUtil::contains('Hello', 'ell');    // true

// Starts with
echo StringUtil::startsWith('Hello', 'He');    // true

// Ends with
echo StringUtil::endsWith('Hello', 'lo');    // true

// Substring
echo StringUtil::substring('Hello', 1, 2);    // el

// Remove
echo StringUtil::remove('Hello', 1, 2);    // Hlo

// Insert
echo StringUtil::insert('Hello World!', 5, ',');    // Hello, World!

// To lower case
echo StringUtil::toLower('Hello');    // hello

// To upper case
echo StringUtil::toUpper('Hello');    // HELLO

// Trim left and right
echo StringUtil::trim(' [Hello] ');    // [Hello]

// Trim left
echo StringUtil::trimStart(' [Hello] ', ' [');    // Hello]

// Trim right
echo StringUtil::trimEnd(' [Hello] ', ' ]');    // [Hello

// Replace
echo StringUtil::replace('Hello, World!', 'o', 'e');    // Helle, Werld!

// Replace by regular expression
echo StringUtil::replaceRegEx('Hello, World!', '/o/', 'e');    // Helle, Werld!

// method chain
echo StringUtil::trim(' [Hello] ')->toLower()->remove(1,2);    // [hlo]

// format
//  - see more samples: https://github.com/stk2k/string-format
echo StringUtil::format('Hello, {0}!', 'David');    // Hello, David!

StringArray

use stk2k\string\StringArray;

$sa = new StringArray(['a', 'b', 'c']);

echo count($sa);                // 3
foreach($sa as $i) echo $i;     // abc
echo $sa->join(',');            // a,b,c
echo $sa->get(1);               // b
echo $sa[1];                    // b
unset($sa[1]);
echo $sa;                       // {"0":"a","2":"c"}
$sa[1] = 'Foo';
echo $sa;                       // {"0":"a","2":"c","1":"Foo"}

StringBuffer

use stk2k\string\StringBuffer;

$b = new StringBuffer('abc');
$c = new StringBuffer('a,b,c');

echo $b->length();                  // 3
echo $c->length();                  // 5
foreach($b as $i) echo $i;          // abc
echo json_encode($c->split(','));   // ["a","b","c"]
echo json_encode($b->split());      // ["a","b","c"]
echo $b->append('d');               // abcd

Requirement

PHP 7.2 or later

Installing stk2k/string

The recommended way to install stk2k/string is through Composer.

composer require stk2k/string

After installing, you need to require Composer's autoloader:

require 'vendor/autoload.php';

License

This library is licensed under the MIT license.

Author

stk2k

Disclaimer

This software is no warranty.

We are not responsible for any results caused by the use of this software.

Please use the responsibility of the your self.