String Functions

Published Apr 17, 2022
Contribute to Docs

PHP has several built-in functions for text processing and the manipulation of strings.

A list of PHP string and text processing functions can be found in the table below:

String Functions

bin2hex()
Converts a string to hexadecimal values.
chr()
Returns a single-character string based on the ASCII value provided as an argument.
echo()
Outputs one or more strings.
explode()
Splits a given string by a delimiter and returns an array of the substrings produced.
hex2bin()
Converts a string of hexadecimal values to its binary representation.
htmlentities()
Converts all applicable characters to HTML entities.
htmlspecialchars()
Returns a converted string of HTML entities.
htmlspecialchars_decode()
Converts special HTML entities to characters.
html_entity_decode()
Converts HTML entities to characters.
implode()
Joins the elements of an array to form a new string
ltrim()
Trims whitespace or specific characters from the left side of a given string.
money_format()
Formats a number and returns it as a currency string.
number_format()
Returns a given value formatted with grouped thousands.
ord()
Returns an integer between 0 and 255 corresponding to the binary value of the first character of a string.
rtrim()
Removes whitespace or other predefined characters from the right side of a string.
sprintf()
Returns a string with a specified formatting.
stripos()
Returns the position of the first occurrence of a substring inside a given string.
strlen()
Returns the length of a string.
strpos()
Performs a case-sensitive search for the first occurrence of a substring in a given string.
strtolower()
Converts a string to lowercase.
strtoupper()
Converts each character in a string to uppercase.
str_contains()
Returns a boolean indicating if the specified string contains the substring provided.
str_ends_with()
Checks if a string ends with a given substring.
str_pad()
Returns a string with a new length by making it longer.
str_repeat()
Returns a string that repeats a substring a specified number of times.
str_replace()
Returns a string with occurrences of a specified substring replaced by another string.
str_shuffle()
Randomly shuffles the characters of a string.
str_starts_with()
Performs a case-sensitive search to check if a string starts with a given substring.
substr()
Returns a portion of a specified string.
trim()
Strips whitespace and/or other characters from the beginning and end of a string.

All contributors

Looking to contribute?

Learn PHP on Codecademy