PHP String Functions

A string is a combination of characters and string functions in a programming language are used to manipulate strings. For example:

$string = “PHP string handling functions.”;

Difference between single and double quoted strings in PHP: In single quoted strings, variables are not interpreted and the whole string is printed as it is. However, in double quoted strings PHP has to parse the variables, examine their values and print accordingly.

Example
<?php
$var = “Sonu Sood”;
echo ‘My name is $var <br>‘;
echo “My name is $var”;
?>

Escape Sequencing in PHP

Escape sequences are used to escape a character while string parsing and giving an additional meaning to it like line breaks, tabs etc. Escape sequences work in double quote strings. Escape sequences start with a backslash(\) followed by a character. Escape sequences used in PHP are as follows :

\’Is replaced by a single quote
\”Is replaced by a double quote
\nTo add line breaks within string
\tTo add tab space
\rFor carriage return
\$Is replaced by a dollar sign itself
<?php
echo “\”PHP Programming.\””;
?>

OUTPUT
“PHP Programming.”

List of PHP String Handling Functions

FunctionDescription
addcslashes()Returns a string after adding backslashes in front of mentioned character
addslashes()Returns a string after adding backslashes in front of predefined characters (‘,”,\,NULL)
bin2hex()bin2hex Converts binary to hexadecimal
chop()Removes whitespace or any other predefined character from the right end of a string
chr()Converts a number to single-byte string
chunk_split()Splits a string into smaller chunks
convert_cyr_string()Converts one cyrillic character set to another
convert_uudecode()Decodes uuencoded string
convert_uuencode()Encodes the string in uuencode algorithm of encoding
count_chars()Information about characters used in the string
crc32()Calculates crc32 of a string
crypt()One way string hashing
echo()Output data or result on screen
explode()Splits a string into an array
fprintf()Writes a formatted string to any stream
get_html_translation_table()Returns the translation table used by htmlspecialchars and htmlentities
hebrev()Converts hebrew text to visual text
hebrevc()Converts hebrew text to visual text and \n to
hex2bin()Converts hexadecimal to binary
html_entity_decode()Converts html entities to characters
htmlentities()Converts characters to html entities
htmlspecialchars_decode()Converts special HTML entities to characters
htmlspecialchars()Converts special characters to HTML entities
implode()Binds an array into a string
join()Binds an array into a string
lcfirst()Converts first character of a string to lowercase
levenshtein()Returns the levenshtein distance between two strings.Number of characters that will be replaced to transform string1 to string 2.
localeconv()Returns numeric formatting information
ltrim()Removes whitespace and other predefined characters from the left side of a string
md5()Calculates md5 hash of a string
md5_file()Calculates md5 hash of a file
metaphone()Calculates metaphone key of a string
money_format()Formats a number as currency
nl_langinfo()Locale information
nl2br()Inserts HTML line breaks in front of all new lines
number_format()Formats to number
ord()Returns the ASCII value of the first character of the string
parse_str()To parse a query string to a variable
print()Used to output string
printf()Prints 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 the whitespace or specified characters from the right side of the string
setlocale()Sets the locale information
sha1()Calculates the SHA-1 hash of a string
sha1_fileCalculates the SHA-1 hash of a file
similar_text()Checks for the similarity between two strings
soundex()Calculates the soundex key of a string
sprintf()Returns a formatted string
sscanf()Converts input from a string as per the given format
str_getcsv()Converts CSV string into an array
str_ireplace()Case insensitive while replacing some characters in a string.
str_pad()Pads a string to specified length
str_repeat()Repeats a string for a specified number of times
str_replace()Replaces specified characters in a string
str_rot13()Encodes a string by ROT13 method
str_shuffle()Shuffles a string randomly
str_split()Splits a string into an array
str_word_count()Counts the number of words in a string
strcasecmp()Case insensitive string comparison
strchr()Checks for the first occurrence of the string
strcmp()Case sensitive string comparison
strcol()Locale based string comparison
strcspn()Returns the count of numbers found in a string before a matching mask
strip_tags()Strips HTML and PHP tags from a string
strpcslashes()Unquotes a string being quoted by addcslashes
stripslashes()Unquotes a quoted string
stripos()Case insensitive search of a string for the first occurrence of a string inside another
stristr()Case-insensitive search for the first occurrence of a string inside another string.
strlen()Returns the length of string
strnatcasecmp()Case insensitive comparison of two strings using Natural Order Algorithm
strnatcmp()Case sensitive comparison of two strings using Natural Order Algorithm
strncasecmp()Case insensitive comparison of strings for first n characters
strncmp()Case sensitive comparison of strings for first n characters
strpbrk()Search a string for any set of characters
strpos()Checks a string for the position of first occurrence of a specified string. Searching is case sensitive
strrchr()Checks a string for the last occurrence of a specified string
strrev()Reverse the string
strripos()Checks a string for the position of last occurrence of a specified string. Searching is case insensitive
strrpos()Checks a string for the position of last occurrence of a specified string. Searching is case sensitive
strspn()Returns the number of characters found in a string which has characters from specified charlist
strstr()Checks for the first occurrence of the string
strtok()Tokenize the string
strtolower()Converts whole string to lowercase
strtoupper()Converts whole string to UPPERCASE
strtr()Translate characters to string
substr()Returns a part of the string
substr_compare()Compare two strings with defined start position
substr_count()Count the number of substring existence in a string
substr_replace()Replaces substring with a defined string
trim()Removes whitespace and predefined characters from both sides of a string
ucfirst()Converts the first character of string to uppercase
ucwords()Converts the first character of each word in a string to uppercase
vfprintf()Writes a formatted string to a stream
vprintf()Prints a formatted string
vsprintf()Returns a formatted string