PHP String functions for Length, Compare, Replace, Find, Uppercase, Lowercase, Search, Reverse

PHP String Functions

PHP String Functions are to manipulate the string. String Length, String Comparison, String Compare, String Replace, String Find, String Uppercase, String Lowercase, String Search are some string functions in PHP.

PHP String Length

String Length Function or strlen()is used to get the length of a string.

Example 1 – PHP String Length Example

<!DOCTYPE html>
<html>
  <head>
   
  </head>
    
  <body>
	<?php
		$x = "Have a nice day.";
		echo "The length of string is.....".strlen($x);
	?>
  </body>
</html>

In the above examples, we create a string variable $x and use the strlen() function to get the length of the string variable.

PHP String Function for Length
Fig.1 – String Length Function Application and Example Syntax Code.

To Get The Number of Word In a String Functions

str_word_count() function is used to get the number of words in a string.

Example 2 – String Word Count Example

<!DOCTYPE html>
<html>
  <head>
   
  </head>
    
  <body>
	<?php
		$x = "Have a nice day.";
		echo "The number of words in a string are...".str_word_count($x);
	?>
  </body>
</html>

In the above examples, we create a string variable $x and use the str_word_count() function to get the numbers of words in the string variable.

PHP String Word Count
Fig.2 – String Word Count Function Example Code.

PHP String Reverse

String Reverse Function or strrev() is used to get the string in a reverse order.

Example 3 – String Reverse Example Code

<!DOCTYPE html>
<html>
  <head>
   
  </head>
    
  <body>
	<?php
		$x = "Have a nice day.";
		echo "The reverse string of (Have a nice day.) is - ".strrev($x);
	?>
  </body>
</html>

In the above examples, we create a string variable $x and use the strrev() function to get the reverse string of that string variable.

PHP String Reverse Function
Fig.3 – String Reverse Function Example Code.

PHP String Find or Search Function Syntax and Code Example

PHP String Find or strpos() is used to find or search a text in a string.

Example 4 – String Search or Find Code

<!DOCTYPE html>
<html>
  <head>
   
  </head>
    
  <body>
	<?php
		$x = "Have a nice day.";
		echo "Nice text is available at the number is....".strpos($x, "nice");
	?>
  </body>
</html>

In the above examples, we have to find the text, here the function strpos() shows on which position that text will appear.

PHP String Search or Find
Fig.4 – String Search Or Find Function Code Output

PHP String Replace

String Replace Function or str_replace() is used to replace some text to another text as per your requirement in a given string.

Example 5 – PHP String Replace Example

<!DOCTYPE html>
<html>
  <head>
   
  </head>
    
  <body>
	<?php
		$x = "Have a nice day.";
		echo "The replacement of Have a nice day. is... ".str_replace("nice", "good", $x);
	?>
  </body>
</html>

In the above examples, we replace the word “nice” to the word “good”.

PHP String Replace
Fig.5 – String Replace Function Example Code Output.

PHP String Uppercase Convert

String Uppercase Function or Strtoupper() is used to convert all the characters of a string into Capital characters or letters.

Example 6 – String Uppercase Convert Example

<!DOCTYPE html>
<html>
  <head>
   
  </head>
    
  <body>
	<?php
		$x = "Have a nice day.";
		echo "The Uppercase of Have a nice day. is... ".Strtoupper($x);
	?>
  </body>
</html>

In the above examples, we all the characters of a string variable $x are convert in a capital characters.

PHP String Uppercase
Fig.6 – String Uppercase Function Example Output.

PHP String Lowercase Convert

String Lowercase Function or Strtolower() is used to convert all the characters of a string into small characters or letters.

Example 7 – String Lowercase Convert Example

<!DOCTYPE html>
<html>
  <head>
   
  </head>
    
  <body>
	<?php
		$x = "HAVE A GOOD DAY.";
		echo "The Lowercase of HAVE A GOOD DAY. is... ".Strtolower($x);
	?>
  </body>
</html>

In the above examples, we all the characters of a string variable $x are convert in a small characters.

PHP String Lowercase
Fig.7 – String Lowercase Function Example Output.

PHP String Compare or Comparison

String Compare Function or strcmp() is used to get comparison between the strings.

It will compare the 2 strings based on the alphabetical order, for example, an alphabet is considered as a number and the function subtract the two numbers. If the strings are same the function will return zero(0) and if the strings are not the same the function will return negative or positive number based on the difference of the length or alphabetical order.

Example 8 – String Compare Example

<!DOCTYPE html>
<html>
  <head>
   
  </head>
    
  <body>
	<?php
		$x = "Have a good day.";
		$y = "Have a nice day.";
		echo strcmp($x, $y);
		
		echo "<br />";
		
		$a = "Elex Tutorial.";
		$b = "CSS Tutorial.";
		echo strcmp($a, $b);
		
		echo "<br />";
		
		$p = "Welcome to the technical world.";
		$q = "Welcome to the technology world.";
		echo strcmp($p, $q);
		
		echo "<br />";
		
		$r = "Hello world";
		$s = "Hello worldsss";
		echo strcmp($r, $s);
		
	?>
  </body>
</html>
PHP String Compare or Comparison
Fig.8 – String Compare Function Example Output.
You can leave a response, or trackback from your own site.

One Response to “PHP String functions for Length, Compare, Replace, Find, Uppercase, Lowercase, Search, Reverse”

  1. some really nice and utilitarian info on this site, besides I conceive the style and design contains excellent features.

Leave a Reply to the article


Learn PHP

Learning & Certifications
Follow Us
Facebook Icon   Linked In Icon   Twitter Icon  
Validation and Recognition

Valid CSS! Valid HTML5!          Protected by Copyscape