In this tutorial, we are going to see 10 basic and important string functions in PHP. String is basically a sequence of character. In this tutorial, we will see basic important PHP string functions which are commonly used in PHP. These inbuilt function in PHP are used to perform different operations while using string. Knowledge of PHP string functions will make programming simpler for you while you have to use string and perform different operations.
PHP String Functions :
In this tutorial, we will get idea of basic PHP string functions.
- Get The length of string.
- Counting number of words in string
- Reverse a string.
- Search text within string.
- Replace/change text within a string.
- Change whole word in UPPERCASE
- Change whole word in LOWERCASE
- Compare Strings
- Get a part of string.
- Strip(Remove) whitespace from string
So let us see every function in detail one by one with its example and you will get where and how to use it in PHP. Here is list of some php string functions which is commonly used.
- Get the Length of String.
PHP have inbuilt function to calculate the length of string. PHP strlen() predefined function is used to get length of string.
<?php
echo strlen(“What is your name”); //output : 17
?>
- Counting Number of Words in String
PHP Have predefined function to count number of words in string.
<?php
echo str_word_count(“What is your name”); //output : 4
?>
- Reverse a string
In PHP language it is not required to write a user define function to reverse a string. PHP have inbuilt function strrev() to reverse a string.
<?php
echo strrev(“phpcluster”); //output : retsulcphp
?>
- Search text within string.
If you want to find a text within string then it is easy to do so with PHP inbuilt function.
<?php
$string = “PHPCluster is a programming blog”;
$findtext = “blog”;
if( strpos( $string, $findtext ) !== false ) {
echo “exists”; //output : exists
}
?>
- Replace text within string
To replace a particular text within a string , there is predefined PHP function.
<?php
$string = “PHPCluster is a programming website”;
$find = “website”;
$replace = “blog”;
echo str_replace($find,$replace,$string); //output : PHPCluster is a programming blog
?>
- Change whole word in UPPERCASE
<?php
echo strtoupper(“vikash kumar singh”); //output : VIKASH KUMAR SINGH
?>
- Change whole word in LOWERCASE
<?php
echo strtolower(“VIKASH KUMAR SINGH”); //output : vikash kumar singh
?>
- Compare Strings
To compare two string there is inbuilt PHP function.
<?php
echo strcmp(“PHPCluster”,”PHPCluster”); //output : 0
?>
If strcmp() function returns 0 , the two values are same(equal).
- Get a part of string.
To get a part of string , PHP have predefined function
Which shows only parts from full string
<?php
echo substr(“PHPCluster Programming Blog”,11); // output : Programming Blog
?>
- Strip(Remove) whitespaces
If it is required to remove all whitespaces from string value , then an inbuilt function in PHP can do it. Trim function can remove unnecessary whitespaces and other character from begining and ends.
<?php
echo trim(“PHPCluster Programming Blog “); //output : PHPCluster Programming Blog
?>
In this tutorial we have covered few important PHP string Functions which are used commonly. PHP have many inbuilt(predefined) function for perform different program. If you have any query , feel free to write comment.