String Functions in PHP

Till now we have learnt what is function? And how can we define user defined functions and how can we execute them? Now we will learn some of the inbuilt functions used in PHP. PHP provides rich set of inbuilt functions which we can use whenever required. As its inbuilt function we need not to define it, it’s already defined all we need to do is just call them whenever required.

Let’s learn the first type of inbuilt function that is String Functions. A string function is used for manipulating strings. There are various string functions which are used to perform some operations with strings.  The different functions are as listed below.

strlen()

This function is used to find the length of given string.

Syntax

int strlen(string)

For Ex.

<?php
    $str="DiscussDesk";
    echo "Length of ".$str." is ".strlen($str);
?>

Output

Length of DiscussDesk is 11

strrev()

This function is used for reversing a given string.

Syntax

string strrev(string)

For Ex.

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

Output

kseDssucsiD

chr()

This function is used to convert the ASCII code values into character value.

Syntax

chr(ASCII Code in integer)

ord()

This function is opposite of chr() it takes a character as input and converts it to associated ASCII value.

Syntax

ord(character)

For Ex.

<?php
    echo chr(65)."<br>"; // it will print A as its ASCII code is 65
    echo ord('A');  // it will print 65
?>

strtolower()

This function returns all characters of strings into lower case.

Syntax

string strtolower(string)

strtoupper()

This function returns all characters of strings into upper case.

Syntax

string strtoupper(string)

ucfirst()

This function will convert the first character into capital letter and rest characters into lower case. In short it will capitalize the first character of given string.

Syntax

string ucfirst(string)

ucwords()

This function will convert the first letter of each word into capital letter(upper case).

Syntax

string ucwords(string)

For Ex.

<?php
    $str="hello discussDesk";
    echo "After strtolower <b>".strtolower($str)."<br></b>";
    echo "After strtoupper <b>".strtoupper($str)."<br></b>";
    echo "After ucfirst <b>".ucfirst($str)."<br></b>";
    echo "After ucwords <b>".ucwords($str)."<br></b>";
?>

Output

After strtolower hello discussdesk
After strtoupper HELLO DISCUSSDESK
After ucfirst Hello discussDesk
After ucwords Hello DiscussDesk

ltrim()

It is abbreviated as left trim. This function is used to remove the unnecessary white spaces from beginning (left) of the string.

Syntax

string ltrim(string variable[, string character])

The first parameter is the variable name for which you need to remove the white spaces. The second parameter is optional and it is used to specify the character/s which are to be removed from the left side of the string.

For Ex.

<?php
    $str1="    DiscussDesk";
    $str2="DiscussDesk";
    echo ltrim($str1)."<br>"; 
    echo ltrim($str2,"Di");	
?>

Output

DiscussDesk
scussDesk

rtrim()

It is abbreviated as right trim. This function is used to remove the unnecessary white spaces from end (right) of the string.

Syntax

string rtrim(string variable[, string character])

The first parameter is the variable name for which you need to remove the white spaces from right side of your string. The second parameter is optional and it is used to specify the character/s which are to be removed from the end (right side) of the string.

For Ex.

<?php
    $str1="DiscussDesk          ";
    $str2="DiscussDesk";
    echo rtrim($str1)."<br>"; 
    echo rtrim($str2,"Desk");	
?>

Outputp

DiscussDesk
Discu

Note :- if you have noticed the output of rtrim($str2,”Desk”) , it will remove all occurrences of characters ‘D’ , ‘e’ , ‘s’ , ‘k’ regardless of their order so it will not remove string “Desk” instead it will remove  all occurrences of characters ‘D’ , ‘e’ , ‘s’ , ‘k’ from the right side of string. Same is the case with ltrim().

trim()

This function is used to remove the unnecessary white spaces from beginning (left) of the string as well as from the end (right side) of the string.

Syntax

string trim(string variable[, string character])

The first parameter is the variable name for which you need to remove the white spaces from both ends of string. The second parameter is optional and it is used to specify the character/s which are to be removed from the end (right side) of the string as well as the beginning (left side) of the string.

For Ex.

<?php
    $str1="            DiscussDesk          ";
    $str2="***DiscussDesk***";
    echo trim($str1)."<br>"; 
    echo trim($str2,"*");	
?>

Output

DiscussDesk
DiscussDesk

So for now we have discussed some of the string functions which are used  very frequently, we will discuss more of them later in our next tutorial.

Share this Article on Social Media

Get Updates, Scripts & Other Tutorials to Directly to your Email

Over 20000+ Happy Readers already subscribed. (We don't send spam email). Every email subscriber can get our latest updates and download our 100+ scripts.

Comments