PHP String Functions

This is an overview of commonly used PHP string functions. Click the function name for more details on what it does, inputs and outputs, examples, and official docs.

Jump to all PHP string functions A-Z.

String + substring

strpos() = Get position of substring in string
substr() = Get substring by position and length
str_replace() = Replace all occurrences of substring
str_ends_with() = Check if string ends with substring
str_starts_with() = Check if string starts with substring

String + array

str_split() = Split string into array of given equal length substrings
explode() = Split string into array by given separator
implode() = Join array items into string with given separator

String manipulation

strtolower() = Convert string to lowercase
strtoupper() = Convert string to uppercase
trim() = Remove whitespace from start and end of string
strip_tags() = Remove HTML and PHP tags from string
str_pad() = Pad string to given length
str_repeat() = Repeat string number of times
strrev() = Reverse string
str_shuffle() = Shuffle string (pseudo randomize order of characters)

String length/words

strlen() = Get string length (number of bytes)
str_word_count() = Get number of words or array of words in string

Function args and details (listed alphabetically)

This list includes commonly used PHP string functions. PHP has many others, most of which 99% people will never use. If you can't find a function here, see full list of PHP string functions in official php.net docs.

explode() = Split $string by $separator into array of parts (docs)

explode(string $separator, string $string, int $limit = PHP_INT_MAX): array

implode() = Join $array elements with $separator into one string (docs)

implode(string $separator, array $array): string
implode(array $array): string

str_ends_with() = Check if $haystack ends with $needle (case sensitive, PHP8+, docs)

str_ends_with(string $haystack, string $needle): bool

str_pad() = Pad $string to $length using $pad_string (docs)

str_pad(
    string $string,
    int $length,
    string $pad_string = " ",
    int $pad_type = STR_PAD_RIGHT
): string

str_repeat() = Repeat $string number of $times (docs)

str_repeat(string $string, int $times): string

str_replace() = Replace all occurrences of $search with $replace in $subject (docs)

str_replace(
    array|string $search,
    array|string $replace,
    string|array $subject,
    int &$count = null
): string|array

str_shuffle() = Pseudo (not crypto-secure!) randomly shuffle $string (docs)

str_shuffle(string $string): string

str_split() = Split $string into array of substrings with $length (docs)

str_split(string $string, int $length = 1): array

str_starts_with() = Check if $haystack starts with $needle (case sensitive, PHP8+, docs)

str_starts_with(string $haystack, string $needle): bool

str_word_count() = Get number of words (if $format=0) or array of words (if $format=1) in $string (docs)

str_word_count(string $string, int $format = 0, ?string $characters = null): array|int

strip_tags() = Strip HTML and PHP tags (except $allowed_tags) from $string (docs)

strip_tags(string $string, array|string|null $allowed_tags = null): string

strlen() = Get $string length as number of bytes, not necessarily characters (docs)

strlen(string $string): int

strpos() = Get position of first $needle in $haystack, false if not found (docs)

strpos(string $haystack, string $needle, int $offset = 0): int|false

strrev() = Reverse $string (docs)

strrev(string $string): string

strtolower() = Convert $string to lowercase (docs for docs)

strtolower(string $string): string

strtoupper() = Convert $string to uppercase (docs)

strtoupper(string $string): string

substr() = Get part of $string starting at $offset with $length number of characters (docs)

substr(string $string, int $offset, ?int $length = null): string

trim() = Remove whitespace or other $characters from start and end of $string (docs)

trim(string $string, string $characters = " 

	vx00"): string