title: SplString meta:

  • name: description content: EasySwoole SplString
  • name: keywords content: swoole|swoole extension|swoole framework|easyswoole,SplString

SplString

Use

Used to process strings.

Method

Method name parameter Description
setString string $string Set string
split int $length = 1 Split a string by length
explode string $delimiter Split a string by separator
subString int $start, int $length Intercept string
encodingConvert string $desEncoding, $detectList = [‘UTF-8’, ‘ASCII’, ‘GBK’,…] Code conversion
utf8 Convert to utf
unicodeToUtf8 Convert unicode encoding to utf-8
toUnicode Convert to unicode encoding (seconds)
compare string $str, int $ignoreCase = 0 Binary string comparison
lTrim string $charList = “ \t\n\r\0\x0B” Delete whitespace characters (or other characters) at the beginning of the string
rTrim string $charList = “ \t\n\r\0\x0B” Delete whitespace characters (or other characters) at the end of the string
trim string $charList = “ \t\n\r\0\x0B” Remove whitespace characters (or other characters) at the beginning and end of the string
pad int $length, string $padString = null, int $pad_type = STR_PAD_RIGHT Fill the string to the specified length with another string
repeat int $times Repeat a string
length Get the length of the string
upper Convert a string to uppercase
lower Convert a string to lowercase
stripTags string $allowable_tags = null Remove HTML and PHP tags from strings
replace string $find, string $replaceTo Substring replacement
between string $startStr, string $endStr Get the intermediate string of the specified target
regex $regex, bool $rawReturn = false Find strings according to regular rules
exist string $find, bool $ignoreCase = true Whether the specified string exists
kebab Convert to skewers
snake string $delimiter = ‘_’ Turn into a snake
studly hump
camel Small hump
replaceArray string $search, array $replace Replace the string in turn
replaceFirst string $search, string $replace Replace the first occurrence of the given value in the string
replaceLast string $search, string $replace Replace the last occurrence of the given value in the string
start string $prefix Start a string with a single instance of a given value
after string $search Returns the rest of the string after the given value
before string $search Get a part of the string before the given value
endsWith $needles Determines if the given string ends with the given substring
startsWith $needles Determine if the given string starts with the given substring

Example

setString

Set the string.

  • string $string Data item index
    1. function setString( string $string ) : SplString

::: warning Example :::

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 10:10
  7. */
  8. require './vendor/autoload.php';
  9. $string = new \EasySwoole\Spl\SplString();
  10. var_dump($string->setString('Hello, EasySwoole')->__toString());
  11. /**
  12. * The output is over:
  13. * string(17) "Hello, EasySwoole"
  14. */

split

Sets the value of an item in the array.

  • int $length Length of each segment
    1. function split( int $length = 1 ) : SplArray

::: warning Example :::

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 10:10
  7. */
  8. require './vendor/autoload.php';
  9. $string = new \EasySwoole\Spl\SplString('Hello, EasySwoole');
  10. var_dump($string->split(5)->getArrayCopy());
  11. /**
  12. * The output is over:
  13. * array(4) {
  14. * [0]=>
  15. * string(5) "Hello"
  16. * [1]=>
  17. * string(5) ", Eas"
  18. * [2]=>
  19. * string(5) "ySwoo"
  20. * [3]=>
  21. * string(2) "le"
  22. * }
  23. */

explode

Split string

  • string $delimiter separator
    1. function explode( string $delimiter ) : SplArray

::: warning Example :::

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 10:10
  7. */
  8. require './vendor/autoload.php';
  9. $string = new \EasySwoole\Spl\SplString('Hello, EasySwoole');
  10. var_dump($string->explode(',')->getArrayCopy());
  11. /**
  12. * The output is over:
  13. * array(2) {
  14. * [0]=>
  15. * string(5) "Hello"
  16. * [1]=>
  17. * string(11) " EasySwoole"
  18. * }
  19. */

subString

Intercept string

  • int $start Starting position
  • int $length Intercept length
    1. function subString( int $start, int $length ) : SplString

::: warning Example :::

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 10:10
  7. */
  8. require './vendor/autoload.php';
  9. $string = new \EasySwoole\Spl\SplString('Hello, EasySwoole');
  10. var_dump($string->subString(0, 5)->__toString());
  11. /**
  12. * The output is over:
  13. * string(5) "Hello"
  14. */

encodingConvert

Code conversion

  • string $desEncoding The encoding format to be converted
  • mixed $detectList Character encoding list
    1. function encodingConvert( string $desEncoding, $detectList = ['UTF-8', 'ASCII', 'GBK', 'GB2312', 'LATIN1', 'BIG5', "UCS-2",] ) : SplString

::: warning Example :::

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 10:10
  7. */
  8. require './vendor/autoload.php';
  9. $string = new \EasySwoole\Spl\SplString('Hello, EasySwoole');
  10. var_dump($string->encodingConvert('UTF-8')->__toString());
  11. /**
  12. * The output is over:
  13. * string(17) "Hello, EasySwoole"
  14. }
  15. */

utf8

Convert to utf-8

  1. function utf8() : SplString

::: warning Example :::

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 10:10
  7. */
  8. require './vendor/autoload.php';
  9. $string = new \EasySwoole\Spl\SplString('Hello, EasySwoole');
  10. var_dump($string->utf8()->__toString());
  11. /**
  12. * The output is over:
  13. * string(17) "Hello, EasySwoole"
  14. }
  15. */

unicodeToUtf8

Convert unicode encoding to utf-8

  1. function unicodeToUtf8() : SplString

::: warning Example :::

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 10:10
  7. */
  8. require './vendor/autoload.php';
  9. $str = '\u4e2d';
  10. $string = new \EasySwoole\Spl\SplString($str);
  11. var_dump($string->unicodeToUtf8()->__toString());
  12. /**
  13. * The output is over:
  14. * string(3) "中"
  15. */

toUnicode

Convert to unicode encoding

  1. function toUnicode() : SplString

::: warning Example :::

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 10:10
  7. */
  8. require './vendor/autoload.php';
  9. $str = '中';
  10. $string = new \EasySwoole\Spl\SplString($str);
  11. var_dump($string->toUnicode()->__toString());
  12. /**
  13. * The output is over:
  14. * string(6) "\U4E2D"
  15. */

compare

Binary string comparison

  • string $str The string to be compared
  • int $ignoreCase Need to be slightly different
    1. function compare( string $str, int $ignoreCase = 0 ) : int

::: warning Example :::

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 10:10
  7. */
  8. require './vendor/autoload.php';
  9. $str = 'test';
  10. $string = new \EasySwoole\Spl\SplString($str);
  11. var_dump($string->compare('apple'));
  12. /**
  13. * The output is over:
  14. * int(19)
  15. */

lTrim

Delete whitespace characters (or other characters) at the beginning of the string

  • string $charList Deleted characters
    1. function lTrim( string $charList = " \t\n\r\0\x0B" ) : SplString

::: warning Example :::

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 10:10
  7. */
  8. require './vendor/autoload.php';
  9. $str = ' test';
  10. $string = new \EasySwoole\Spl\SplString($str);
  11. var_dump($string->lTrim()->__toString());
  12. /**
  13. * The output is over:
  14. * string(4) "test"
  15. */

rTrim

Delete whitespace characters (or other characters) at the end of the string

  • string $charList Deleted characters
    1. function rTrim( string $charList = " \t\n\r\0\x0B" ) : SplString

::: warning Example :::

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 10:10
  7. */
  8. require './vendor/autoload.php';
  9. $str = 'test ';
  10. $string = new \EasySwoole\Spl\SplString($str);
  11. var_dump($string->rTrim()->__toString());
  12. /**
  13. * The output is over:
  14. * string(4) "test"
  15. */

trim

Remove whitespace characters (or other characters) at the beginning and end of the string

  • string $charList Deleted characters
    1. function trim( string $charList = " \t\n\r\0\x0B" ) : SplString

::: warning Example :::

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 10:10
  7. */
  8. require './vendor/autoload.php';
  9. $str = ' test ';
  10. $string = new \EasySwoole\Spl\SplString($str);
  11. var_dump($string->trim()->__toString());
  12. /**
  13. * The output is over:
  14. * string(4) "test"
  15. */

pad

Fill the string to the specified length with another string

  • int $length Value is negative, less than or equal to the length of the input string, no padding will occur
  • string $padString Fills the string
  • int $pad_type Fill type

    1. function pad( int $length, string $padString = null, int $pad_type = STR_PAD_RIGHT ) : SplString

    $pad_type type:

  • STR_PAD_RIGHT Right padding

  • STR_PAD_LEFT Left padding
  • STR_PAD_BOTH Left and right padding

::: warning Example :::

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 10:10
  7. */
  8. require './vendor/autoload.php';
  9. $str = 'test';
  10. $string = new \EasySwoole\Spl\SplString($str);
  11. var_dump($string->pad(5, 'game')->__toString());
  12. /**
  13. * The output is over:
  14. * string(5) "testg"
  15. */

repeat

Repeat a string

  • int $times repeat times
    1. function repeat( int $times ) : SplString

::: warning Example :::

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 10:10
  7. */
  8. require './vendor/autoload.php';
  9. $str = 'test';
  10. $string = new \EasySwoole\Spl\SplString($str);
  11. var_dump($string->repeat(2)->__toString());
  12. /**
  13. * The output is over:
  14. * string(8) "testtest"
  15. */

length

Get the length of the string

  1. function length() : int

::: warning Example :::

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 10:10
  7. */
  8. require './vendor/autoload.php';
  9. $str = 'test';
  10. $string = new \EasySwoole\Spl\SplString($str);
  11. var_dump($string->length());
  12. /**
  13. * The output is over:
  14. * int(4)
  15. */

upper

Convert a string to uppercase

  1. function upper() : SplString

::: warning Example :::

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 10:10
  7. */
  8. require './vendor/autoload.php';
  9. $str = 'test';
  10. $string = new \EasySwoole\Spl\SplString($str);
  11. var_dump($string->upper()->__toString());
  12. /**
  13. * The output is over:
  14. * string(4) "TEST"
  15. */

lower

Convert a string to lowercase

  1. function lower() : SplString

::: warning Example :::

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 10:10
  7. */
  8. require './vendor/autoload.php';
  9. $str = 'test';
  10. $string = new \EasySwoole\Spl\SplString($str);
  11. var_dump($string->lower()->__toString());
  12. /**
  13. * The output is over:
  14. * string(4) "test"
  15. */

stripTags

Remove HTML and PHP tags from strings

  • string $allowable_tags Specify a list of characters that are not removed
    1. function stripTags( string $allowable_tags = null ) : SplString

::: warning Example :::

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 10:10
  7. */
  8. require './vendor/autoload.php';
  9. $str = '<a>test</a>';
  10. $string = new \EasySwoole\Spl\SplString($str);
  11. var_dump($string->stripTags()->__toString());
  12. /**
  13. * The output is over:
  14. * string(4) "test"
  15. */

replace

Substring replacement

  • string $find Target value of lookup
  • string $replaceTo Replacement value
    1. function replace( string $find, string $replaceTo ) : SplString

::: warning Example :::

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 10:10
  7. */
  8. require './vendor/autoload.php';
  9. $str = 'test';
  10. $string = new \EasySwoole\Spl\SplString($str);
  11. var_dump($string->replace('t', 's')->__toString());
  12. /**
  13. * The output is over:
  14. * string(4) "sess"
  15. */

between

Get the intermediate string of the specified target

  • string $startStr Specify the starting string of the target
  • string $endStr Specify the end string of the target
    1. function between( string $startStr, string $endStr ) : SplString

::: warning Example :::

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 10:10
  7. */
  8. require './vendor/autoload.php';
  9. $str = 'easyswoole';
  10. $string = new \EasySwoole\Spl\SplString($str);
  11. var_dump($string->between('easy', 'le')->__toString());
  12. /**
  13. * The output is over:
  14. * string(4) "swoo"
  15. */

regex

Find strings according to regular rules

  • mixed $regex Regular rule
  • bool $rawReturn Whether to return the original string
    1. public function regex( $regex, bool $rawReturn = false )

::: warning Example :::

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 10:10
  7. */
  8. require './vendor/autoload.php';
  9. $str = 'easyswoole';
  10. $string = new \EasySwoole\Spl\SplString($str);
  11. var_dump($string->regex('/swoole/'));
  12. /**
  13. * The output is over:
  14. * string(6) "swoole"
  15. */

exist

Whether the specified string exists

  • string $find Find string
  • bool $ignoreCase Ignore case
    1. public function exist( string $find, bool $ignoreCase = true ) : bool

::: warning Example :::

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 10:10
  7. */
  8. require './vendor/autoload.php';
  9. $str = 'easyswoole';
  10. $string = new \EasySwoole\Spl\SplString($str);
  11. var_dump($string->exist('Swoole', true));
  12. /**
  13. * The output is over:
  14. * bool(true)
  15. */

kebab

Convert to skewers

  1. function kebab() : SplString

::: warning Example :::

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 10:10
  7. */
  8. require './vendor/autoload.php';
  9. $str = 'EasySwoole';
  10. $string = new \EasySwoole\Spl\SplString($str);
  11. var_dump($string->kebab()->__toString());
  12. /**
  13. * The output is over:
  14. * string(11) "easy-swoole"
  15. */

snake

Turn into a snake

  • string $delimiter Separator
    1. function snake( string $delimiter = '_' ) : SplString

::: warning Example :::

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 10:10
  7. */
  8. require './vendor/autoload.php';
  9. $str = 'EasySwoole';
  10. $string = new \EasySwoole\Spl\SplString($str);
  11. var_dump($string->kebab()->__toString());
  12. /**
  13. * The output is over:
  14. * string(11) "easy_swoole"
  15. */

studly

Hump

  1. function studly() : SplString

::: warning Example :::

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 10:10
  7. */
  8. require './vendor/autoload.php';
  9. $str = 'easy_swoole';
  10. $string = new \EasySwoole\Spl\SplString($str);
  11. var_dump($string->studly()->__toString());
  12. /**
  13. * The output is over:
  14. * string(10) "EasySwoole"
  15. */

camel

Small hump

  1. function camel() : SplString

::: warning Example :::

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 10:10
  7. */
  8. require './vendor/autoload.php';
  9. $str = 'easy_swoole';
  10. $string = new \EasySwoole\Spl\SplString($str);
  11. var_dump($string->camel()->__toString());
  12. /**
  13. * The output is over:
  14. * string(10) "easySwoole"
  15. */

replaceArray

Replace the string for each element of the array

  • string $search Find string
  • array $replace Replacement target
    1. public function replaceArray( string $search, array $replace ) : SplString

::: warning Example :::

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 10:10
  7. */
  8. require './vendor/autoload.php';
  9. $str = 'easy_swoole';
  10. $string = new \EasySwoole\Spl\SplString($str);
  11. var_dump($string->replaceArray('easy', ['as', 'bs', 'cs'])->__toString());
  12. /**
  13. * The output is over:
  14. * string(9) "as_swoole"
  15. */

replaceFirst

Replace the first occurrence of the given value in the string

  • string $search Find string
  • string $replace Replace string
    1. public function replaceFirst( string $search, string $replace ) : SplString

::: warning Example :::

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 10:10
  7. */
  8. require './vendor/autoload.php';
  9. $str = 'easy_swoole_easy';
  10. $string = new \EasySwoole\Spl\SplString($str);
  11. var_dump($string->replaceFirst('easy', 'as')->__toString());
  12. /**
  13. * The output is over:
  14. * string(14) "as_swoole_easy"
  15. */

replaceLast

Replace the last occurrence of the given value in the string

  • string $search Find string
  • string $replace Replace string
    1. public function replaceLast( string $search, string $replace ) : SplString

::: warning Example :::

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 10:10
  7. */
  8. require './vendor/autoload.php';
  9. $str = 'easy_swoole_easy';
  10. $string = new \EasySwoole\Spl\SplString($str);
  11. var_dump($string->replaceLast('easy', 'as')->__toString());
  12. /**
  13. * The output is over:
  14. * string(14) "easy_swoole_as"
  15. */

start

Start a string with a single instance of a given value

  • string $prefix Opening string
    1. public function start( string $prefix ) : SplString

::: warning Example :::

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 10:10
  7. */
  8. require './vendor/autoload.php';
  9. $str = 'EasySwoole';
  10. $string = new \EasySwoole\Spl\SplString($str);
  11. var_dump($string->start('Hello,')->__toString());
  12. /**
  13. * The output is over:
  14. * string(16) "Hello,EasySwoole"
  15. */

after

Returns the rest of the string after the given value

  • string $search Find string
    1. function after( string $search ) : SplString

::: warning Example :::

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 10:10
  7. */
  8. require './vendor/autoload.php';
  9. $str = 'EasySwoole';
  10. $string = new \EasySwoole\Spl\SplString($str);
  11. var_dump($string->after('Easy')->__toString());
  12. /**
  13. * The output is over:
  14. * string(6) "Swoole"
  15. */

before

Get a part of the string before the given value

  • string $search Find string
    1. function before( string $search ) : SplString

::: warning Example :::

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 10:10
  7. */
  8. require './vendor/autoload.php';
  9. $str = 'EasySwoole';
  10. $string = new \EasySwoole\Spl\SplString($str);
  11. var_dump($string->before('Swoole')->__toString());
  12. /**
  13. * The output is over:
  14. * string(4) "Easy"
  15. */

endsWith

Determines if the given string ends with the given substring

  • string $needles Find string
    1. public function endsWith( $needles ) : bool

::: warning Example :::

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 10:10
  7. */
  8. require './vendor/autoload.php';
  9. $str = 'EasySwoole';
  10. $string = new \EasySwoole\Spl\SplString($str);
  11. var_dump($string->endsWith('Swoole'));
  12. /**
  13. * The output is over:
  14. * bool(true)
  15. */

startsWith

Determine if the given string starts with the given substring

  • string $needles Find string
    1. public function startsWith( $needles ) : bool

::: warning Example :::

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 10:10
  7. */
  8. require './vendor/autoload.php';
  9. $str = 'EasySwoole';
  10. $string = new \EasySwoole\Spl\SplString($str);
  11. var_dump($string->startsWith('Easy'));
  12. /**
  13. * The output is over:
  14. * bool(true)
  15. */