10 Basic PHP String Functions

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.

10 Basic PHP String Functions

PHP String Functions :

In this tutorial, we will get idea of basic PHP string functions.

  1. Get The length of string.
  2. Counting number of words in string
  3. Reverse a string.
  4. Search text within string.
  5. Replace/change text within a string.
  6. Change whole word in UPPERCASE
  7. Change whole word in LOWERCASE
  8. Compare Strings
  9. Get a part of string.
  10. 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.

 

  1. 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

?>

  1. 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

?>

 

  1. 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

?>

 

  1. 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

 

}

?>

 

  1. 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

 

?>

 

  1. Change whole word in UPPERCASE

 

<?php

echo strtoupper(“vikash kumar singh”);   //output  : VIKASH KUMAR SINGH

?>

 

  1.  Change whole word in LOWERCASE

 

<?php

echo strtolower(“VIKASH KUMAR SINGH”);   //output  : vikash kumar singh

?>

 

  1. 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).

 

  1. 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

?>

 

  1. 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.

Leave a Comment