Ad

PHP String Functions

A String is a collection of characters. A string variable can contain alphabets, digits, and special characters. In PHP, we can perform some operation on string using built in PHP string functions.

Built-in PHP String functions are:
S.No.FunctionDescription
strlen() Returns the length of a string.
str_word_count() Counts the number of words in a string.
strrev() Reverses the string.
strpos() Searches for a specific text within a string. If found, returns the character position of the first match. If no match is found, it will return FALSE.
strchr() Searches for the first occurrence of a string. Returns the string if found.
str_replace() Replace string with another string.
Syntax: echo str_replace("old string", "new string", "big sentence containing old string");
strcmp() Compare two strings (case-sensitive):
Syntax: echo strcmp("Hello world!","Hello world!");
strcasecmp() Compares two strings (case-insensitive)
strncmp() String comparison of the first n characters (case-sensitive)
strtolower() Converts a string to lowercase letters
strtoupper() Converts a string to uppercase letters
ucfirst() Converts the first character of a string in uppercase
ucwords() Converts the first character of each word in a string in uppercase
str_shuffle() Shuffles all characters in a string randomly.
substr() Returns a part(sub-string) within a string.
str_repeat() Repeats a string up to specified number of times.
trim() Removes whitespace from both sides of a string
wordwrap() Wraps a string to a given number of characters
explode() Break a string into an array on the bases of "separator" parameter.
htmlentities() Convert tags into HTML entities

Example 1: String Length

$a = "PHP is a scripting language";
$len = strlen($a);
echo "String Length is $len";
String Length is 27

Example 2: String word count

$a = "PHP is a scripting language";
$count = str_word_count($a);
echo "Total words are $count";
Total words are 5

Example 3: String Reverse

$a = "PHP is a scripting language";
$rev = strrev($a);
echo "String Reverse = $rev";
String Reverse = egaugnal gnitpircs a si PHP

Example 4.1: Find string position

$a = "PHP is a scripting language";
$pos = strpos($a, "scripting");
echo "Position of scripting in string is $pos";
Position of scripting in string is 8

Example 4.2: Find string position with proper message.

$a = "PHP is a scripting language";
$search = "is";
$pos = strpos($a, $search);
if($pos===false)
    echo "Not found";
else
    if($pos>=0)
        echo "String found at $pos position";
String found at 4 position
Advertisement

Advertisement

Example 5: Strchr() function

$a = "PHP is a client side scripting language";
$search = "scripting ";
echo strchr($a, $search);
scripting language

Example 6: Replace all found string.

$a = "PHP is a client side scripting language";
//str_replace("word to be find", "new word to be replace with", "string");
echo str_replace("PHP", "JavaScript", $a);

Example 7: Compare 2 strings

$str1="new delhi";
$str2="new york";
//$str3=strcmp($str1,$str2);
//$str3=strcasecmp($str1,$str2); //ignore cases
$str3 = strncmp($str1,$str2,4);
if($str3==0){
    echo "Both strings are equal";
}
else
if($str3>0){
    echo "1st strings is greater";
}
else{
    echo "2nd strings is greater";
}

Example 8: Convert string into lowercase.

$a = "PHP is a client side scripting language";
$b = strtolower($a);
$c = strtoupper($a);
echo "<br>String in Lowercase: ".$b;
echo "<br>String in Uppercase: ".$c;

Example 9: Converts the first character of a string in uppercase.

$a = "PHP is a client side scripting language";
echo "<br>ucfirst: ".ucfirst($a);
echo "<br>ucwords: ".ucwords($a);

Example 10: Shuffles all characters in a string randomly.

$a = str_shuffle("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()");
echo "<br>After Shuffle String is: ".$a;

Example 11: substr() function

$a = "PHP is a client side scripting language";
//substr("string", "starting value", "length");
echo substr($a, 9, 6);//returns client

Example 12: str_repeat() function

echo str_repeat("-",80);// display '-' hyphen 80 times

Example 13: trim() function

$a = "     PHP    String     ";
echo "<br>". $a;
echo "<br>". trim($a);

Example 14: wordwrap() function

$a = "PHP is a client side scripting language";
echo "<br>".wordwrap($a, 5, "<br>");

Example 15: explode() function

$a = "PHP is a client side scripting language";
//explode(separator,string);
$b = explode(" ",$a)
echo "<br>First word is: ".$a;

Example 16: htmlentities() function

$a = "<br> is use for break line";
echo htmlentities($a);//convert < with &lt;

Exercise 1: CREATE a login form & check for username and password. is valid or not.(strcmp) also check the password length (strlen) should be in between 8-32. username is not case sensitive. (strcasecmp/strtolower). Assume username = admin/ADMIN/Admin and password = admin123