Home >>PHP String Functions

PHP String Functions

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

Here's example illustrating these operators in action


Eg ii ( strrev( ) )

<?php
	
$val="nitin";
	
if(strrev($val)==$val)
	
echo "Your name is palindrome";
	
else
	
echo "Your name is not palindrome";
  
?>

Output
Your name is 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);
  
 ?>

Output
welcome welcome welcome

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 : [email protected]@ because "@" replaced by "e".


Eg v ( str_word_count( ) )

<?php
  
$str="hello  user how r you";
  
echo str_word_count($str);
  
?>

Output
5

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);  
?>
Output
1

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>

Output
name must be greater than 5
Enter your name

Eg viii ( strpos( ) )

<?php
       
$str="welcome";
       
echo strpos($str,"l");
  
?>

Output
2

Eg ix ( nl2br( ) )

<?php
	
$str1="hello 
	
user
	
how 
	
are
	
you";
	
echo nl2br($str1);
  
?>
Output
hello
user
how
are
you

Eg x( substr( ) )

<?php
	
$str="welcome to the world of php";
	
echo substr($str,24,3);
 
?>

Output
php


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


PHP String Functions Index

Sr.No. Topics
1 PHP addcslashes() Function
2 PHP addslashes() Function
3 PHP bin2hex() Function
4 PHP chop() Function
5 PHP chr() Function
6 PHP chunk_split() Function
7 PHP convert_cyr_string() Function
8 PHP convert_uudecode() Function
9 PHP convert_uuencode() Function
10 PHP count_chars() Function
11 PHP crc32() Function
12 PHP crypt() Function
13 PHP echo() Function
14 PHP empty() function
15 PHP explode() Function
16 PHP strcmp() Function
17 PHP fprintf() Function
18 PHP strcoll() Function
19 PHP get_html_translation_table() Function
20 PHP strcspn() Function
21 PHP hebrev() Function
22 PHP strip_tags() Function
23 PHP hebrevc() Function
24 PHP stripcslashes() Function
25 PHP hex2bin() Function
26 PHP stripslashes() Function
27 PHP html_entity_decode() Function
28 PHP stripos() Function
29 PHP htmlentities() Function
30 PHP stristr() Function
31 PHP htmlspecialchars() Function
32 PHP strlen() Function
33 PHP htmlspecialchars_decode() Function
34 PHP strnatcasecmp() Function
35 PHP implode() Function
36 PHP strnatcmp() Function
37 PHP join() Function
38 PHP strncasecmp() Function
39 PHP lcfirst() Function
40 PHP strncmp() Function
41 PHP levenshtein() Function
42 PHP strpos() Function
43 PHP localeconv() Function
44 PHP strpbrk() Function
45 PHP ltrim() Function
46 PHP strrev() Function
47 PHP md5() Function
48 PHP strripos() Function
49 PHP md5_file() Function
50 PHP strrpos() Function
51 PHP metaphone() Function
52 PHP strspn() Function
53 PHP money_format() Function
54 PHP strstr() Function
55 PHP nl_langinfo() Function
56 PHP strtok() Function
57 PHP nl2br() Function
58 PHP strtolower() Function
59 PHP number_format() Function
60 PHP strtoupper() Function
61 PHP ord() Function
62 PHP strtr() Function
63 PHP parse_str() Function
64 PHP substr() Function
65 PHP print() Function
66 PHP substr_compare() Function
67 PHP printf() Function
68 PHP substr_count() Function
69 PHP quoted_printable_decode() Function
70 PHP substr_replace() Function
71 PHP quoted_printable_encode() Function
72 PHP trim() Function
73 PHP quotemeta() Function
74 PHP ucfirst() Function
75 PHP rtrim() Function
76 PHP ucwords() Function
77 PHP setlocale() Function
78 PHP vfprintf() Function
79 PHP sha1() Function
80 PHP vprintf() Function
81 PHP sha1_file() Function
82 PHP vsprintf() Function
83 PHP similar_text() Function
84 PHP wordwrap() Function
85 PHP soundex() Function
86 PHP strrchr Function
87 PHP sprintf() Function
88 PHP sscanf() Function
89 PHP str_getcsv() Function
90 PHP str_ireplace() Function
91 PHP str_pad() Function
92 PHP str_repeat() Function
93 PHP str_replace() Function
94 PHP str_rot13() Function
95 PHP str_shuffle() Function
96 PHP str_split() Function
97 PHP str_word_count() Function
98 PHP strcasecmp() Function
99 PHP strchr() Function

PHP String Functions PHP addcslashes() Function PHP addslashes() Function PHP bin2hex() Function PHP chop() Function PHP chr() Function PHP chunk_split() Function PHP convert_cyr_string() Function PHP convert_uudecode() Function PHP convert_uuencode() Function PHP count_chars() Function PHP crc32() Function PHP crypt() Function PHP echo() Function PHP empty() function PHP explode() Function PHP strcmp() Function PHP fprintf() Function PHP strcoll() Function PHP get_html_translation_table() Function PHP strcspn() Function PHP hebrev() Function PHP strip_tags() Function PHP hebrevc() Function PHP hex2bin() Function PHP html_entity_decode() Function PHP htmlentities() Function PHP htmlspecialchars() Function PHP htmlspecialchars_decode() Function PHP implode() Function PHP join() Function PHP lcfirst() Function PHP levenshtein() Function PHP localeconv() Function PHP ltrim() Function PHP md5() Function PHP md5_file() Function PHP metaphone() Function PHP money_format() Function PHP nl_langinfo() Function PHP nl2br() Function PHP number_format() Function PHP ord() Function PHP parse_str() Function PHP print() Function PHP printf() Function PHP quoted_printable_decode() Function PHP quoted_printable_encode() Function PHP quotemeta() Function PHP rtrim() Function PHP setlocale() Function PHP sha1() Function PHP sha1_file() Function PHP similar_text() Function PHP soundex() Function PHP sprintf() Function PHP sscanf() Function PHP str_getcsv() Function PHP str_ireplace() Function PHP str_pad() Function PHP str_repeat() Function PHP str_replace() Function PHP str_rot13() Function PHP str_shuffle() Function PHP str_split() Function PHP str_word_count() Function PHP strcasecmp() Function PHP strchr() Function