Home >>PHP String Functions
PHP has over 75 built-in String manipulation functions, supporting operations ranging from string repetition and reversal to comparison and search-and-replace. Some of these important functions are
Function | Description |
---|---|
addcslashes() | This function returns a string with backslashes before any specified characters present in the string |
addslashes() | Escapes special characters in a string with backslashes |
bin2hex() | This function is used to converts any string of characters in to hexadecimal values |
chop() | Deletes the whitespace or other characters present in the string from the end of a string |
chr() | Used to get the specific character value of a ascii value. |
chunk_split() | Divides any given string into many smaller parts of defined length |
convert_cyr_string() | Used for the convertion of any given string between different Cyrillic character-set |
convert_uudecode() | This function is used to Decodes any string which is uuencoded |
convert_uuencode() | This function is used to encode any given string with the help of uuencode algorithm |
count_chars() | This function gives all information about the characters present in the string |
crc32() | This function is used for the calculation of a 32-bit CRC for the given string |
crypt() | This function return a hashed string using different algorithms like MD5,Blowfish. |
echo() | This function is used to print one or more string as the output |
empty() | Tests if a string is empty |
explode() | This function is used to make an array from the given string |
fprintf() | This function is used to print the given string in different formatted string |
get_html_translation_table() | This function is used to get the translation table used by htmlspecialchars() and htmlentities() |
hebrev() | This function is used to convert the Hebrew logical text into visual text |
hebrevc() | This function is used for the Convertion of Hebrew logical text into visual text and new lines(\n) into break<br> |
hex2bin() | This function is used for the convertion of any string of hexadecimal values into ASCII characters |
html_entity_decode() | Decodes HTML entities within a string |
htmlentities() | Encodes HTML within a string |
htmlspecialchars_decode() | Decodes special HTML characters withing a string |
htmlspecialchars() | Encodes special HTML characters within a sting |
implode() | Converts the elements of an array into a string. |
join() | Alias of implode() |
lcfirst() | This function Changes the first character of the given string in lowercase |
levenshtein() | This function gives the Levenshtein distance between the two strings |
localeconv() | This function gives the locale numeric and monetary formatting information |
ltrim() | This function eliminates the whitespaces or other characters from the left side of the given string |
md5() | The MD5 message-digest algorithm is a widely used for cryptography |
md5_file() | This function calculates the MD5 hash for a file |
metaphone() | This function is used for the Calculation of the metaphone key of a given string |
money_format() | This function gives a string formatted as a currency string |
nl_langinfo() | This function gives the specific local information |
nl2br() | Replaces line breaks in a string with elements |
number_format() | This function is used to format a number with grouped thousands |
ord() | This function gives the ASCII value of the first character of any given string |
parse_str() | This function is used for the Parsing of a query string into variables |
print() | This function print the Output as one or more strings |
printf() | This function is used to print the Output as a formatted string |
quoted_printable_decode() | This function Converts any given quoted-printable string into an 8-bit string |
quoted_printable_encode() | This function Converts an given 8-bit string into a quoted printable string |
quotemeta() | This function is used to Quote meta characters |
rtrim() | This function eliminates whitespaces or other characters from the right side of the given string |
setlocale() | This function is used to Set the locale information |
sha1() | This function is used to Calculate the SHA-1 hash of the given string |
sha1_file() | This function is used to Calculate the SHA-1 hash of any given file |
similar_text() | This function is used to check the similarity between the two strings |
soundex() | This function is used to calculate the soundex key of the string |
sprintf() | This function changes any formatted string to a variable |
sscanf() | This function is used for the Parsing of any input from a string according to a format |
str_getcsv() | This function is used for the Parsing of a given CSV string into an array |
str_ireplace() | This function is used to Replace some characters of a string |
str_pad() | This function is used for the Padding of a string to a new length |
str_repeat() | Repeats a string as many number of times you want |
str_replace() | Replaces parts of a string |
str_rot13() | This function is used for the ROT13 encoding on a string |
str_shuffle() | This function Randomly shuffles all characters of the given string |
str_split() | str_split() function splits(break) a string into an array. |
str_word_count() | Calculates the number of words in a string |
strcasecmp() | Compares two strings |
strchr() | This function Finds the very first presence of a string inside another string |
strcmp() | Compares two strings |
strcoll() | Compares two strings based on locale of the strings |
strcspn() | Counts the number of characters found in a string |
strip_tags() | Removes PHP and HTML code from a string |
stripcslashes() | Removes backslashes before any specified character from a string |
stripslashes() | Removes backslashes from a string |
stripos() | This function gives the position of the first presence of a string inside another string |
stristr() | This function finds the first presence of a string inside another string |
strlen() | Calculates the number of characters in a string |
strnatcasecmp() | Comparison between two strings using the "natural order" algorithm(case-insensitive) |
strnatcmp() | Comparison between two strings using the "natural order" algorithm(case-sensitive) |
strncasecmp() | Compares the first n characters between two strings(case-insensitive) |
strncmp() | Compares the first n characters between two strings(case-sensitive) |
strpbrk() | Looks for a set of characters in any string |
strpos() | Gives the position of the first presence of a string inside any other string(case-sensitive) |
strrchr() | Gives the last presence of a string inside another string |
strrev() | Retrun reverse of a given string |
strripos() | Locates the position of the last presence of a string inside another string(case-insensitive) |
strrpos() | Locates the position of the last presence of a string inside another string(case-sensitive) |
strspn() | Gives the count of the characters found in a given string that contains only characters from a specified charlist |
strstr() | Locates the first presence of a string inside another string(case-sensitive) |
strtok() | Breaks a string into different smaller strings |
strtolower() | Converts in Lowercases a string |
strtoupper() | Converts in Uppercases a string |
strtr() | Translates some specified characters into a string |
substr() | Retrieves a section of a string |
substr_compare() | Comparision between two strings from a specified start position |
substr_count() | Gives the number of times a substring occur in a given string |
substr_replace() | Replaces any specified part of a string with another string |
trim() | Removes leading and trailing whitespaces from a string |
ucfirst() | Converts in uppercase the first character of a string |
ucwords() | Converts in uppercases the first character of every word of a string |
vfprintf() | Converts a formatted type string to as specified output |
vprintf() | Gives a formatted string as Output |
vsprintf() | Converts a formatted type string into a variable |
wordwrap() | Wrap a string into new line after specified number of characters |
Eg ii ( strrev( ) )
<?php $val="nitin"; if(strrev($val)==$val) echo "Your name is palindrome"; else echo "Your name is not palindrome"; ?>
In the above example,
Declare variable $val hold value="nitin". Here we use strrev() Function to give reverse of a string.
We pass strrev() function inside If condition. if the reverse string is equal to declared string.
it will print "Your name is palindrome" otherwise "Your name is not palindrome"
Eg iii (str_repeat( ) )
<?php $val="welcome "; echo str_repeat($val,3); ?>
In the above example,
Declare variable $val with value="welcome".
use str_repeat( ) function with two argument. first argument declare name of variable, second argument we define number of times print the value.
The output is (welcome welcome welcome) because we pass 3 second argument.
Eg iv ( str_replace( ) )
<?php $str="welcome"; echo str_replace("e","@",$str); ?>
In the above example,
Declare variable $str with value="welcome".
use str_replace( ) function. It accepts three argument: the search term, the replacement term, and the string on which perform replacement.
we have Passed str_replace("e","@",$str) and the output is : W@lcom@ because "@" replaced by "e".
Eg v ( str_word_count( ) )
<?php $str="hello user how r you"; echo str_word_count($str); ?>
In the above example,
Use str_word_count( ) function is used to count the number of word in a string.
declare variable $str value="hello user how are you".
pass this function inside echo so the output is :5 (count words separated by space)
Eg vi ( strcmp( ) )
<?php $str="hello"; $str1="HELLO"; echo strcmp($str,$str1); ?>
In the above example,
declare two variable $str value=("hello")
$str1 with value=("HELLO")
Now compare two string using strcmp( ) function.
display the output i.e 1 because both variable doesn't contain same value(one is in lowercase while other in uppercase).
Eg vii(strlen( ))
<?php if(isset($_GET['sub'])) { if(empty($_GET['n'])) { echo "<font color='red'>fill your name first</font>"; } else { if(strlen($_GET['n'])<5) { echo "<font color='red'>name must be greater than 5</font>"; } else { echo "welcome ".$_GET['n']; } } } ?> <form> Enter your name<input type="text" name="n"/> <input type="submit" name="sub" value="show my name"/> </form>
Eg viii ( strpos( ) )
<?php $str="welcome"; echo strpos($str,"l"); ?>
Eg ix ( nl2br( ) )
<?php $str1="hello user how are you"; echo nl2br($str1); ?>
Eg x( substr( ) )
<?php $str="welcome to the world of php"; echo substr($str,24,3); ?>
In the above example,
substr( ) function is used to slice string into smaller section.
it accepts three argument: (given string ,the position at which start slicing , and the number of character return from the starting position ).
$str hold a string value="welcome to the world of php" now pass substr($str,24,3) function inside echo and the output will become: php