Begin Web Programming with PHP & MySQL

Advertisements



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);

Eg:
<?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.

Eg:
<?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);

Eg:
<?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);

Eg:
<?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.
Eg:
<?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.

Eg:
<?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);

Eg:
<?php
echo strtolower("HeLlO"); // output - hello
?>
Output:
hello

strtoupper()

The strtoupper() function converts an entire string to uppercase.
Syntax:
strtoupper(string);

Eg:
<?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);

Eg:
<?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);

Eg:
<?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);

Eg:
<?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);

Eg:
<?php
$str = "Hello world"; echo lcfirst($str); // returns - hello world
?>
Output:
hello world