PHP String Functions
A string is a sequence of characters. String is also a data type in PHP used to store text. Some commonly used functions to manipulate strings are discussed here.
echo()
The echo() function outputs one or more strings.
Syntax:
echo(strings);
<?php
echo "hello world";
?>
Output:
hello world
explode()
It is used to break a string into an array.
Syntax:
explode(separator,string,limit);
Here 'separator' specifies where to break the string and 'string' is the text to split.
<?php
$str = "Apple, Orange, Banana, Kiwi";
$arr = explode(",",$str); // converts string to array
echo $arr[0]; // will output Apple
?>
Output:
Apple
implode()
It is used to return a string from the elements of an array (opposite to explode()).
Syntax:
implode(separator,string);
<?php
$arr = array("Hello,", "how", "are", "you?");
$str = implode(" ",$arr);// converts array to string with space as separator
echo $str; // output - Hello, how are you?
?>
Output:
Hello, how are you?
md5()
The md5() function calculates the 128-bit MD5 hash of a string. It is the 5th version of the Message-Digest Algorithm and produces the message digest through five steps. We use md5() function mainly for encrypting the passwords while storing them in the database.
Syntax:
md5(string, true/false);
<?php
$password = "jamesbond";
$enc_password = md5($password);
echo $enc_password;
?>
Output:
e84c55c90d955bf1cfa2d31a1f425383
strcmp()
The strcmp() function compares two strings and it is case-sensitive.
Syntax:
strcmp(string1,string2);
strcmp() returns:
- 0 - if the two strings are equal.
- < 0 - if string1 is less than string2.
- > 0 - if string1 is greater than string2.
<?php
echo strcmp("Ganga Expressway","Ganga Expressway"); /* returns 0 strings are equal */
echo "<br>";
echo strcmp("Ganga Expressway","Ganga"); /* string1 is greater than string2 */
echo "<br>";
echo strcmp("Ganga","Ganga Expressway"); /* string1 is less than string2 */
?>
Output:
0
1
-1
strlen()
The strlen() function returns the length of a string.
<?php
echo strlen("Hello world"); /* returns 11, space is also counted*/
?>
Output:
11
strtolower()
The strtolower() function converts an entire string to lowercase.
Syntax:
strtolower(string);
<?php
echo strtolower("HeLlO"); // output - hello
?>
Output:
hello
strtoupper()
The strtoupper() function converts an entire string to uppercase.
Syntax:
strtoupper(string);
<?php
echo strtoupper("HeLlO"); // output - HELLO
?>
Output:
HELLO
str_replace()
The str_replace() returns a string or an array with the replaced values. It replaces all the occurrences of the search string or array.
Syntax:
str_replace(find,replace,string,count);
<?php
$str = "Hello, how are you?";
$replaced_str = str_replace('Hello', 'Hi', $str);
print_r($replaced_str); // prints Hi, how are you?
?>
Output:
Hi, how are you?
substr()
The substr() in PHP is a function used to extract a part of the given string. It returns the extracted part of the string or an empty string.
Syntax:
substr(string,start-position,length);
<?php
$str = "Hello, how are you?";
echo substr($str,15,3); // returns - you
?>
The Starting position of the substring is 15. There are 15 characters including space until 'y'. Length 3 means 'y','o' and 'u' are returned as a string.
Output:
you
ucfirst()
The ucfirst() function converts the first character of a string to uppercase.
Syntax:
ucfirst(string);
<?php
$str = "hello world";
echo ucfirst($str); // returns - Hello world
?>
Output:
Hello world
lcfirst()
The lcfirst() function converts the first character of a string to lowercase.
Syntax:
lcfirst(string);
<?php
$str = "Hello world"; echo lcfirst($str); // returns - hello world
?>
Output:
hello world