X

Rate Us :



Share with Friends :

Menu

String Functions in PHP:

String is a collection of characters, there are number of string functions are available to work with string :


strlen() :

By using this function, we can get the length of an characters of a string.

Example:

<?php $str = "Welcome"; echo strlen($str); ?>

Output:

7

strtoupper() :

By using this function, we can convert all characters of a string into upper case characters.

Example:

<?php $str = "Welcome"; echo strtoupper($str); ?>

Output:

WELCOME

strtolower() :

By using this function, we can convert all characters of a string into lower case characters.

Example:

<?php $str = "Welcome"; echo strtolower($str); ?>

Output:

welcome

chr() :

By using this function, we can get the character of an ASCII value.

Example:

<?php echo chr(97); ?>

Output:

a

ord() :

By using this function, we can get the ASCII of input character.

Example:

<?php echo ord('A'); ?>

Output:

65

ucfirst() :

By using this function, we can convert first character of a string into upper case character.

Example:

<?php $str = "welcome"; echo ucfirst($str); ?>

Output:

Welcome

ucwords() :

By using this function, we can convert first character of all words into upper case character.

Example:

<?php $str = "welcome to NY"; echo ucwords($str); ?>

Output:

Welcome To NY

nl2br() :

By using this function, we can break new lines of a string.

Example:

<?php $str = "Welcome \n to \n PHP"; echo nl2br($str); ?>

Output:

Welcome to PHP

addslashes() :

By using this function, we can add backslashes within the single quotations & double quotations are occurred.

Example:

<?php $str = "Wel'come"; $str1 = addslashes($str); echo $str1; ?>

Output:

Wel\'come

stripslashes() :

By using this function, we can remove the slashes which we added with addslashes().

Example:

<?php $str = "Wel'come"; $str1 = addslashes($str); echo $str1; echo "</br>"; echo stripslashes($str); ?>

Output:

Wel\'come Wel'come

str_word_count() :

By using this function, we can get the total number of words of a string. We need to pass mode value as second argument. If mode is zero(0), it returns total number of words. If mode is one(1), it returns each word as an Array. Array values of words and keys are 0,1,2... If mode is 2, it returns all words as Array values are the words of string & keys are the index number of the string.

Example:

<?php $str = "Welcome to NY"; print_r(str_word_count($str, 1)); ?>

Output:

Array ( [0] => Welcome [1] => to [2] => NY )

similar_text() :

By using this function, we can get the similarities between two string.

Example:

<?php $str = "Welcome to NY"; $str1 = "Welcome to NY"; echo similar_text($str, $str1); ?>

Output:

13 It indicates that all the characters including spaces are matched.

join() :

By using this function, we can join Array elements as a string. It is same as implode().

Example:

<?php $arr = array("Master", "Mind"); echo join("/",$arr); ?>

Output:

Master/Mind

trim() :

By using this function, we can remove left hand side & right hand side spaces of a string.


ltrim() :

By using this function, we can remove left hand side spaces of a string.


rtrim() :

By using this function, we can remove right hand side spaces of a string.


chop() :

It is an alias of rtrim.

Example:

<?php $str = "Welcome "; $str1 = " Man"; echo trim($str).ltrim($str1); ?>

Output:

WelcomeMan

str_shuffle() :

By using this function, we can randomly shuffles a string.

Example:

<?php $str = "Welcome"; echo str_shuffle($str); ?>

Output:

elWcmoe (randomly shuffles the string)

str_replace() :

By using this function, we can replaces a part of string with a new string.

Example:

<?php $str = "Welcome"; echo str_replace("come", "go", $str); ?>

Output:

Welgo

str_ireplace() :

By using this function, we can replaces a part of string with a new string same as str_replace but it is case insensitive.

Example:

<?php $str = "Welcome"; echo str_ireplace("Come", "go", $str); ?>

Output:

Welgo

str_repeat() :

By using this function, we can repeats a string with a specified number of times.

Example:

<?php $str = "Welcome"; echo str_repeat($str, 3); ?>

Output:

WelcomeWelcomeWelcome

str_split() :

By using this function, we can convert a string as an Array.

Example:

<?php $str = "Welcome to NY"; $arr = str_split($str, 5); // 5 indicates the maximum number of characters stored in a particular index of array print_r($arr); ?>

Output:

Array ( [0] => Welco [1] => me to [2] => NY )

strcmp() :

By using this function, we can compares two strings and returns '0', if both are same, it returns greater than '0', if first string is greater than second string. It returns less than '0', if first string less than second string.

Example:

<?php $str = "Welcome"; $str1 = "Welcome"; echo strcmp($str, $str1); ?>

Output:

0

strcasecmp() :

It is same as strcmp. But it is case insensitive.

Example:

<?php $str = "Welcome"; $str1 = "WELCOME"; echo strcasecmp($str, $str1); ?>

Output:

0

strchr() :

By using this function, we can get all characters of a string from specified character.


strstr() :

It is same as strchr().


stristr() :

It is same as strstr(), but case insensitive.

Example:

<?php $str = "Welcome"; echo strchr($str, "c"); echo "<br>"; echo strstr($str, "c"); echo "<br>"; echo stristr($str, "C"); ?>

Output:

come come come

strrchr() :

By using this function, we can get all characters of a string from specified character same as strchr().

Example:

<?php $str = "Welcome"; echo strrchr($str, "c"); ?>

Output:

come

strpos() :

By using this function, we can find the index of character in a string.


stripos():

By using this function, we can find the index of character in a string same as strpos() but it is case insensitive.

Example:

<?php $str = "Welcome"; echo strpos($str, "l"); echo "
"; echo stripos($str, "L"); ?>

Output:

2 2

substr() :

By using this function, we can get the substring of a string.

Example:

<?php $str = "Welcome"; echo substr($str, 3); ?>

Output:

come

strip_tags() :

By using this function, we can strip the string from html tags.

Example:

<?php $str = "Hello <h1>World</h1>"; echo strip_tags($str); ?>

Output:

Hello World

strrev() :

By using this function, we can convert the string into reverse direction.

Example:

<?php $str = "Welcome"; echo strrev($str); ?>

Output:

emocleW