PHP Function Reference

PHP - Strings



The PHP has a number of functions that are available for use with strings. These functions manipulate strings in various ways. Some more specialized sections can be found in the regular expression and URL handling sections.

Installation

There is no installation needed to use these functions. These functions are part of the PHP core.

Runtime Configuration

This extension has no configuration directives defined in php.ini.

PHP String Functions

FunctionsDescription
addcslashes() Quote predefined characters of a string with slashes in a C style.
addslashes() Quote predefined characters of a string with slashes.
bin2hex() Converts a string of ASCII characters into hexadecimal representation.
chop() Removes whitespace (or other characters) from the right end of a string. Alias of rtrim() function.
chr() Returns a character from a specified ASCII value
chunk_split() Splits a string into smaller chunks.
convert_cyr_string() Converts a string from one Cyrillic character set to another.
convert_uudecode() Decodes a uuencoded string.
convert_uuencode() Encodes a string using the uuencode algorithm.
count_chars() Returns information about characters used in a string.
crc32() Calculates a 32-bit CRC for a string
crypt() One-way string hashing.
echo() Output one or more strings.
explode() Breaks a string into an array.
fprintf() Writes a formatted string to a specified output stream.
get_html_translation_table() Returns the translation table used by htmlspecialchars() and htmlentities().
hebrev() Convert logical Hebrew text to visual text.
hebrevc() Convert logical Hebrew text to visual text with newline conversion.
hex2bin() Converts a string of hexadecimal values into ASCII characters.
html_entity_decode() Converts HTML entities to characters.
htmlentities() Converts characters to HTML entities.
htmlspecialchars_decode() Converts some predefined HTML entities to characters.
htmlspecialchars() Converts some predefined characters to HTML entities.
implode() Returns a string from the elements of an array separated by specified separator.
join() Alias of implode(). Returns a string from the elements of an array separated by specified separator.
lcfirst() Converts the first character of a string to lowercase.
levenshtein() Returns the Levenshtein distance between two strings.
localeconv() Returns locale numeric and monetary formatting information.
ltrim() Removes whitespace (or other characters) from the left side of a string.
md5() Calculates the md5 hash of a string.
md5_file() Calculates the MD5 hash of a given file.
metaphone() Calculates the metaphone key of a string.
money_format() Returns a string formatted as a currency string.
nl_langinfo() Returns specific local information.
nl2br() Inserts HTML line breaks in front of each newline in a string.
number_format() Formats a number with grouped thousands.
ord() Returns the ASCII value of the first character of a string.
parse_str() Parses a query string into variables.
print() Output a string.
printf() Output a formatted string.
quoted_printable_decode() Converts a quoted-printable string to an 8-bit string.
quoted_printable_encode() Converts an 8-bit string to a quoted printable string.
quotemeta() Quotes meta characters.
rtrim() Removes whitespace (or other characters) from the right side of a string.
setlocale() Sets locale information.
sha1() Calculates the SHA-1 hash of a string.
sha1_file() Calculates the SHA-1 hash of a given file.
similar_text() Calculates the similarity between two strings.
soundex() Calculates the soundex key of a string.
sprintf() Returns a formatted string.
sscanf() Parses input from a string according to a format.
str_contains() Determine if a string contains a given substring.
str_ends_with() Checks if a string ends with a given substring.
str_getcsv() Parses a CSV string into an array.
str_ireplace() Replaces some characters in a string (case-insensitive).
str_pad() Pads a string to a new length.
str_repeat() Repeats a string a specified number of times.
str_replace() Replaces some characters in a string (case-sensitive).
str_rot13() Performs the ROT13 encoding on a string.
str_shuffle() Randomly shuffles all characters in a string.
str_split() Splits a string into an array.
str_starts_with() Checks if a string starts with a given substring.
str_word_count() Counts the number of words in a string.
strcasecmp() Compares two strings (case-insensitive).
strchr() Finds the first occurrence of a string inside another string. Alias of strstr() function.
strcmp() Compares two strings (case-sensitive).
strcoll() Compares two strings (locale based string comparison).
strcspn() Returns the length of initial segment in a string not matching specified charlist.
strip_tags() Strips HTML and PHP tags from a string.
stripcslashes() Unquotes a string quoted with addcslashes().
stripslashes() Unquotes a string quoted with addslashes().
stripos() Returns the position of the first occurrence of a string inside another string (case-insensitive).
stristr() Finds the first occurrence of a string inside another string (case-insensitive).
strlen() Returns the length of a string.
strnatcasecmp() Compares two strings using a "natural order" algorithm (case-insensitive).
strnatcmp() Compares two strings using a "natural order" algorithm (case-sensitive).
strncasecmp() String comparison of the first n characters (case-insensitive).
strncmp() String comparison of the first n characters (case-sensitive).
strpbrk() Searches a string for any of a set of characters.
strpos() Returns the position of the first occurrence of a string inside another string (case-sensitive).
strrchr() Finds the last occurrence of a string inside another string.
strrev() Reverses a string.
strripos() Finds the position of the last occurrence of a string inside another string (case-insensitive).
strrpos() Finds the position of the last occurrence of a string inside another string (case-sensitive).
strspn() Returns the length of the initial segment of a string consisting entirely of characters contained within a specified charlist.
strstr() Finds the first occurrence of a string inside another string (case-sensitive).
strtok() Splits a string into smaller strings (tokens).
strtolower() Converts a string to lowercase letters.
strtoupper() Converts a string to uppercase letters.
strtr() Translates certain characters in a string.
substr() Returns a part of a string.
substr_compare() Compares two strings from a specified start position (binary safe and optionally case-sensitive).
substr_count() Counts the number of times a substring occurs in a string.
substr_replace() Replaces a part of a string with another string.
trim() Removes whitespace or other characters from both sides of a string.
ucfirst() Converts the first character of a string to uppercase.
ucwords() Converts the first character of each word in a string to uppercase.
vfprintf() Writes a formatted string to a specified output stream.
vprintf() Outputs a formatted string.
vsprintf() Writes a formatted string to a variable.
wordwrap() Wraps a string to a given number of characters.

PHP String Predefined Constants

The constants below are defined by this extension, and will only be available when the extension has either been compiled into PHP or dynamically loaded at runtime.

ConstantsTypeDescription
CRYPT_SALT_LENGTHInteger
CRYPT_STD_DESIntegerIndicates whether standard DES-based hashes are supported in crypt(). Always 1.
CRYPT_EXT_DESIntegerIndicates whether extended DES-based hashes are supported in crypt(). Always 1.
CRYPT_MD5IntegerIndicates whether MD5 hashes are supported in crypt(). Always 1.
CRYPT_BLOWFISHIntegerIndicates whether Blowfish hashes are supported in crypt(). Always 1.
CRYPT_SHA256IntegerIndicates whether SHA-256 hashes are supported in crypt(). Always 1.
CRYPT_SHA512IntegerIndicates whether SHA-512 hashes are supported in crypt(). Always 1.
HTML_SPECIALCHARSInteger
HTML_ENTITIESInteger
ENT_COMPATInteger
ENT_QUOTESInteger
ENT_NOQUOTESInteger
ENT_IGNOREInteger
ENT_SUBSTITUTEInteger
ENT_DISALLOWEDInteger
ENT_HTML401Integer
ENT_XML1Integer
ENT_XHTMLInteger
ENT_HTML5Integer
CHAR_MAXInteger
LC_CTYPEInteger
LC_NUMERICInteger
LC_TIMEInteger
LC_COLLATEInteger
LC_MONETARYInteger
LC_ALLInteger
LC_MESSAGESInteger
STR_PAD_LEFTInteger
STR_PAD_RIGHTInteger
STR_PAD_BOTHInteger