string functions
7 methodsProcedural string functions provided by the PHP standard library.
strlen(string $string): intReturns the length of the given string in bytes (not characters).
Parameters
| Name | Type | Description |
|---|---|---|
| string | string | The input string. |
Returns
int
Example
php
<?php
echo strlen("hello"); // 5
echo strlen("日本語"); // 9 (UTF-8 bytes)strpos(string $haystack, string $needle, int $offset = 0): int|falseFinds the numeric position of the first occurrence of needle in haystack. Returns false if not found.
Parameters
| Name | Type | Description |
|---|---|---|
| haystack | string | String to search in. |
| needle | string | Substring to find. |
| offset | int | Search starting offset (default 0). |
Returns
int|false
Example
php
<?php
$pos = strpos("Hello, World", "World");
if ($pos !== false) {
echo $pos; // 7
}substr(string $string, int $offset, ?int $length = null): stringReturns the portion of string specified by offset and length.
Parameters
| Name | Type | Description |
|---|---|---|
| string | string | Input string. |
| offset | int | Starting position (negative counts from end). |
| length | ?int | Maximum length (null = to end). |
Returns
string
Example
php
<?php
echo substr("Hello, World", 7); // "World"
echo substr("Hello, World", 7, 3); // "Wor"
echo substr("Hello", -3); // "llo"str_replace(array|string $search, array|string $replace, string|array $subject): string|arrayReplaces all occurrences of search with replace in subject.
Parameters
| Name | Type | Description |
|---|---|---|
| search | array|string | Value(s) being searched for. |
| replace | array|string | Replacement value(s). |
| subject | string|array | String/array being searched. |
Returns
string|array
Example
php
<?php
echo str_replace("world", "PHP", "hello world"); // "hello PHP"
echo str_replace(["a", "b"], "x", "abc"); // "xxc"strtolower(string $string): stringReturns string with all ASCII alphabetic characters converted to lowercase.
Parameters
| Name | Type | Description |
|---|---|---|
| string | string | Input string. |
Returns
string
Example
php
<?php
echo strtolower("HELLO World"); // "hello world"explode(string $separator, string $string, int $limit = PHP_INT_MAX): arraySplits a string by a separator and returns an array of the pieces.
Parameters
| Name | Type | Description |
|---|---|---|
| separator | string | Boundary string. |
| string | string | Input string. |
| limit | int | Maximum number of elements. |
Returns
array
Example
php
<?php
$parts = explode(",", "a,b,c");
// ["a", "b", "c"]
$parts2 = explode(",", "a,b,c", 2);
// ["a", "b,c"]trim(string $string, string $characters = " \n\r\t\v\x00"): stringStrips whitespace (or other characters) from the beginning and end of a string.
Parameters
| Name | Type | Description |
|---|---|---|
| string | string | Input string. |
| characters | string | Optional custom characters to strip. |
Returns
string
Example
php
<?php
echo trim(" hello "); // "hello"
echo trim("---hi---", "-"); // "hi"