PHPackages                             stk2k/xstring - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. stk2k/xstring

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

stk2k/xstring
=============

Basic string library

0.2.7(4y ago)0551MITPHPPHP &gt;=7.2

Since Jun 26Pushed 4y ago1 watchersCompare

[ Source](https://github.com/stk2k/xstring)[ Packagist](https://packagist.org/packages/stk2k/xstring)[ Docs](https://github.com/stk2k/xstring)[ RSS](/packages/stk2k-xstring/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependencies (4)Versions (10)Used By (1)

Basic string library
====================

[](#basic-string-library)

[![Latest Version on Packagist](https://camo.githubusercontent.com/4ceb6f1f482fd292896ec48ab9ccf2236dcc4a18dd4105a699027ca9cf9a7ce4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f73746b326b2f78737472696e672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/stk2k/xstring)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/2f4e59498ccaeb6824c414667c3517a0c40917ec8374fbf3452834d9dc8ac913/68747470733a2f2f6170692e7472617669732d63692e636f6d2f73746b326b2f78737472696e672e7376673f6272616e63683d6d61696e)](https://api.travis-ci.com/stk2k/xstring.svg?branch=main)[![Coverage Status](https://camo.githubusercontent.com/623b0f8734c5f4c94eec1cbc5c8b8aaf6cc660e0d2d319d9fe1ced04bb2d2b8a/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f73746b326b2f78737472696e672f62616467652e7376673f6272616e63683d6d61696e)](https://coveralls.io/repos/github/stk2k/xstring/badge.svg?branch=main)[![Code Climate](https://camo.githubusercontent.com/45cf3d4bd806b6a3de6542f7b72cde06e81bc02ff5dee1fd4c5401edc51c4d17/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f73746b326b2f78737472696e672f6261646765732f6770612e737667)](https://codeclimate.com/github/stk2k/xstring)[![Total Downloads](https://camo.githubusercontent.com/270fc270769733f9dae040ffd8da81cfb8c04e2404f6d2db77915c3c7b82ea8f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f73746b326b2f78737472696e672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/stk2k/xstring)

Description
-----------

[](#description)

Basic string library

Feature
-------

[](#feature)

- Supports ascii string and multibyte string
- Provide facade interface(xs)

Usage
-----

[](#usage)

### Facade interface(xs)

[](#facade-interfacexs)

```
use stk2k\xstring\xs;

// Length
echo xs::length('Hello');    // 5
echo xs::length('你好');    // 2

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// foreach
xs::each('Hello', function($c){
    echo $c . '.';    // H.e.l.l.o.
});

// match
echo xs::match('Hello, World!', '/lo/');                      // ["lo"]
echo xs::match('Foo123, Bar456, Foo789', '/Foo([0-9]+)/');    // ['Foo123','123']
```

### global function(s)

[](#global-functions)

```
use function stk2k\xstring\globals\s;

echo s('Hello');                // Hello
echo s('Hello')->length();      // 5
echo s('Hello')->toLower();     // hello

// foreach
foreach(s('Hello') as $c){
    echo $c . '.';    // H.e.l.l.o.
}
```

### xStringArray

[](#xstringarray)

```
use stk2k\xstring\xStringArray;

$sa = new xStringArray(['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"}
```

### xStringBuffer

[](#xstringbuffer)

```
use stk2k\xstring\xStringBuffer;

$b = new xStringBuffer('abc');
$c = new xStringBuffer('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
-----------

[](#requirement)

PHP 7.2 or later

Installing stk2k/xstring
------------------------

[](#installing-stk2kxstring)

The recommended way to install stk2k/xstring is through [Composer](http://getcomposer.org).

```
composer require stk2k/xstring
```

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

```
require 'vendor/autoload.php';
```

License
-------

[](#license)

This library is licensed under the MIT license.

Author
------

[](#author)

[stk2k](https://github.com/stk2k)

Disclaimer
----------

[](#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.

###  Health Score

21

—

LowBetter than 18% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity42

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1 days

Total

9

Last Release

1775d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/955f3d564811dc5e1212c1a7a66bc4eb7e11ac166cc38600f244dc25f97d3ef4?d=identicon)[stk2k](/maintainers/stk2k)

---

Top Contributors

[![stk2k](https://avatars.githubusercontent.com/u/985640?v=4)](https://github.com/stk2k "stk2k (31 commits)")

---

Tags

phpstring

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/stk2k-xstring/health.svg)

```
[![Health](https://phpackages.com/badges/stk2k-xstring/health.svg)](https://phpackages.com/packages/stk2k-xstring)
```

###  Alternatives

[coduo/php-to-string

Simple library that converts PHP value into strings

27112.7M10](/packages/coduo-php-to-string)[iteks/laravel-enum

A comprehensive Laravel package providing enhanced enum functionalities, including attribute handling, select array conversions, and fluent facade interactions for robust enum management in Laravel applications.

2516.7k](/packages/iteks-laravel-enum)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
