Oihana PHP

slice.php

Table of Contents

Functions

slice()  : string
Extracts a substring from a UTF-8 encoded string using grapheme clusters, which ensures multi-byte characters (like emojis, accented letters) are not broken.

Functions

slice()

Extracts a substring from a UTF-8 encoded string using grapheme clusters, which ensures multi-byte characters (like emojis, accented letters) are not broken.

slice(string|null $source[, int $start = 0 ][, int|null $length = null ]) : string

If the source is null, it returns an empty string.

Parameters
$source : string|null

The input string or null.

$start : int = 0

The start position (0-based). Negative values count from the end.

$length : int|null = null

The length of the substring. Defaults to max int (whole string from $start).

Tags
example
use function oihana\core\strings\slice;

echo slice ( "Hello World", 6 ) ; // Outputs: "World"

echo slice ( "πŸ‘©β€πŸ‘©β€πŸ‘§β€πŸ‘§ family", 0, 5 ) ; // Outputs: "πŸ‘©β€πŸ‘©β€πŸ‘§β€πŸ‘§"

echo slice ( null ) ; // Outputs: ""

echo slice ( "Hello" , -3 , 2 ) ; // Outputs: "ll"
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
string

The extracted substring or empty string if $source is null.


        
On this page

Search results