PHP String Functions

PHP provides various string functions to access and manipulate strings.

PHP String Functions

addcslashes() It is used to return a string with backslashes.
addslashes() It is used to return a string with backslashes.
bin2hex() It is used to convert a string of ASCII characters to hexadecimal values.
chop() It removes whitespace or other characters from the right end of a string.
chr() It is used to return a character from a specified ASCII value.
chunk_split() It is used to split a string into a series of smaller parts.
chunk_split() It is used to split a string into a series of smaller parts.
convert_cyr_string() It is used to convert a string from one Cyrillic character-set to another.
convert_uudecode() It is used to decode a uuencoded string.
convert_uuencode() It is used to encode a string using the uuencode algorithm.
count_chars() It is used to return information about characters used in a string.
crc32() It is used to calculate a 32-bit CRC for a string.
crypt() It is used to create hashing string One-way.
echo() It is used for output one or more strings.
explode() It is used to break a string into an array.
fprint() It is used to write a formatted string to a stream.
get_html_translation_table() Returns translation table which is used by htmlspecialchars() and htmlentities().
hebrev() It is used to convert Hebrew text to visual text.
hebrevc() It is used to convert Hebrew text to visual text and new lines (n) into <br>.
hex2bin() It is used to convert string of hexadecimal values to ASCII characters.
htmlentities() It is used to convert character to HTML entities.
html_entity_decode() It is used to convert HTML entities to characters.
htmlspecialchars() Converts special characters to HTML entities.
htmlspecialchars_decode() Converts HTML entities back to special characters.
implode() It is used to return a string from the elements of an array.
join() It is the alias of implode() function.
levenshtein() It returns the Levenshtein distance between two strings.
lcfirst() It converts the first character of a string to lowercase.
localeconv() Gets numeric formatting information.
ltrim() It removes whitespace from the left side of a string.
md5() It calculates the MD5 hash of a string.
md5_file() It calculates the MD5 hash of a file.
metaphone() It is used to calculate the metaphone key of a string.
money_format() It is used to return a string formatted as a currency string.
nl2br() It is used to insert HTML line breaks in front of each newline in a string.
number_format() It is used to format a number with grouped thousands.
ord() It is used to return ASCII value of the first character of a string.
print() It is used for outputting one or more strings.
printf() It is used to show output as a formatted string.
quoted_printable_decode() Converts a quoted-printable string to an 8-bit string.
quoted_printable_encode() Converts the 8-bit string back to a quoted-printable string.
substr_replace() Replaces part of a string with another substring.
trim() Removes whitespace or other characters from the beginning and end of a string.
ucfirst() Converts the first character of a string to uppercase.
wordwrap() Wraps a string to a given number of characters.

PHP String Function Examples

1) PHP strtolower() function

The strtolower() function returns string in lowercase letter.

Syntax


string strtolower ( string $string )  

Example


<?php  
$str="My name is KHAN";  
$str=strtolower($str);  
echo $str;  
?>  

Output

my name is khan

2) PHP strtoupper() function

The strtoupper() function returns string in uppercase letter.

Syntax


string strtolower ( string $string )  
    

Example


<?php  
$str="My name is KHAN";  
$str=strtoupper($str);  
echo $str;  
?>  

Output

MY NAME IS KHAN

3) PHP ucfirst() Function

The ucfirst() function returns a string with the first character converted to uppercase. It doesn’t change the case of other characters.

Syntax


string ucfirst ( string $str )  

Example


<?php  
$str = "my name is KHAN";  
$str = ucfirst($str);  
echo $str;  
?>

Output

My name is KHAN

4) PHP lcfirst() function

The lcfirst() function returns string converting first character into lowercase. It doesn't change the case of other characters.

Syntax


string lcfirst ( string $str )  

Example


<?php  
$str="MY name IS KHAN"; 
$str=lcfirst($str);  
echo $str;    
?>

Output

My name is KHAN

5) PHP ucwords() function

The ucwords() function returns string converting first character of each word into uppercase.

Syntax


string ucwords ( string $str )  

Example


<?php  
$str="my name is Sonoo jaiswal";  
$str=ucwords($str);  
echo $str;      
?>

Output

My Name Is Sonoo Jaiswal

6) PHP strrev() function

The strrev() function returns reversed string.

Syntax


string strrev ( string $string )  

Example


<?php  
$str="my name is Sonoo jaiswal";  
$str=strrev($str);  
echo $str;       
?>

Output

lawsiaj oonoS si eman ym

7) PHP strlen() function

The strlen() function returns length of the string.

Syntax


int strlen ( string $string )  
 

Example


<?php  
$str="my name is Sonoo jaiswal";  
$str=strlen($str);  
echo $str;       
?>

Output

24