Thursday, May 7, 2020

PHP String Functions

In this section, we will discuss a few basic string functions which are most commonly used in PHP scripts.

1. Getting length of a String: PHP has a predefined function to get the length of a string. Strlen() displays the length of any string. It is more commonly used in validating input fields where the user is limited to enter a fixed length of characters.

<?php
echo strlen("Welcome to Cloudways");    //will return the length of given string
?>

Output

20

 

2. Counting of the number of words in a String: Another function that enables the display of the number of words in any specific string is str_word_count(). This function is also useful in the validation of input fields.

<?php
echo str_word_count("Welcome to Cloudways");
//will return the number of words in a string
?>
 

Output

3


3. Reversing a String: Strrev() is used for reversing a string. You can use this function to get the reverse version of any string.

<?php
echo strrev(“Welcome to Cloudways”);// will return the string starting from the end
?>

Output

syawduolC ot emocleW


4. Finding Text Within a String: Strpos() enables searching particular text within a string. It works simply by matching the specific text in a string. If found, then it returns the specific position. If not found at all, then it will return “False”. Strops() is most commonly used in validating input fields like email.

<?php
echo strpos(“Welcome to Cloudways”,”Cloudways”);
?>

Output

11

 

5. Replacing text within a string: Str_replace() is a built-in function, basically used for replacing specific text within a string.

<?php
echo str_replace(“cloudways”, “the programming world”, “Welcome to cloudways”);
?>

Output

Welcome to the programming world

 

6. Converting lowercase into Title Case: Ucwords() is used to convert first alphabet of every word into uppercase.

<?php
echo ucwords(“welcome to the php world”);
?>

Output

Welcome To The Php World

 

7. Converting a whole string into UPPERCASE: Strtoupper() is used to convert a whole string into uppercase.

<?php
echo strtoupper(“welcome to cloudways”);// It will convert all letters of string 
                                            into uppercase
?>

Output

WELCOME TO CLOUDWAYS

 

8. Converting whole String to lowercase: Strtolower() is used to convert a string into lowercase.

<?php
echo strtolower(“WELCOME TO CLOUDWAYS”);

?>

Output

welcome to cloudways

 

9. Repeating a String: PHP provides a built-in function for repeating a string a specific number of times.

<?php
echo str_repeat(“=”,13);

?>

Output

=============


10. Comparing Strings: You can compare two strings by using strcmp(). It returns output either greater than zero, less than zero or equal to zero. If string 1 is greater than string 2 then it returns greater than zero. If string 1 is less than string 2 then it returns less than zero. It returns zero, if the strings are equal.

<?php
echo strcmp(“Cloudways”,”CLOUDWAYS”);
echo “<br>”;
echo strcmp(“cloudways”,”cloudways”);//Both the strings are equal
echo “<br>”;
echo strcmp(“Cloudways”,”Hosting”);
echo “<br>”;
echo strcmp(“a”,”b”);//compares alphabetically
echo “<br>”;
echo strcmp(“abb baa”,”abb baa caa”);//compares both strings and returns the result 
                                        in terms of number of characters.
?>

Output

1

0

-1

-1

-4

 

11. Displaying part of String: Through substr() function you can display or extract a string from a particular position.

<?php
echo substr(“Welcome to Cloudways”,6).”<br>”;
echo substr(“Welcome to Cloudways”,0,10).”<br>”;
?>

Output

e to Cloudways

Welcome to

 

12. Removing white spaces from a String: Trim() is dedicated to remove white spaces and predefined characters from a both the sides of a string.

<?php
$str = “Wordpess Hosting”;
echo $str . “<br>”;
echo trim(“$str”,”Wording”);
?>

Output

Wordpess Hosting

pess Host