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.
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 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 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 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 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 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 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>
some really nice and utilitarian info on this site, besides I conceive the style and design contains excellent features.