PHP

Some Important String Functions In PHP

String in PHP is a set of characters. In PHP string can be created using either single quote or double quote.PHP provides a number of built-in string functions which help in performing several operations while dealing with data.

Single Quote Vs Double Quote String.
$str1 = 'This is string example within single quote';
$str2 = "This is string example within double quote";

String written inside double quote will replace the variable values if any whereas single quote string will print the string as it is.

Example.

<?php
$name = 'PHP';
echo 'This is $name';  // output :  This is $name.
echo  "This is $name"; // output : This is PHP.
?>

Some important functions of string.

1. strlen() :

This functions is used for finding the length of string.

<?php 
         
         $str  = "This is PHP";
         echo "Length of the string". strlen($str). 

?>

Output : Length of the string 11 .

2.str_word_count() :

This function is used for counting the total number of words in a string.

<?php
 
$str  = "Welcome To Virtual World";
echo str_word_count($str);

?>

Output : 4 .

3.strrev() :

This function is used to reverse a string.

<?php
       $str = "Welcome To Virtual World";
       echo strrev($str);
?>

Output : dlroW lautriV oT emocleW.

4.strpos($str,$pos_str).

This function is used to find the position of any string in a given string. It takes two parameter first one is string in which we want to search a string and second one is value which we want to search.

<?php
$str ='Hellworld';
echo strpos($str,'l');

?>

Output : 2 .

5.strcmp($str1,$str2).

This function is used to compare two strings. It compares the string alphabetically. If the first string is greater than second string, the result will be greater than 0, If the first string is equal to the second string, the result will be equal to 0 and If the second string is greater than to the first string, the result will be less than 0.

<?php
$str1 = 'Hello';
$str2 = 'Hello World';
echo strcmp($str1,$str2);  // Output:  -6

echo  strcmp($str2,$str1); //Output : 6

echo strcmp($str2,Str2); // output : 0 

?>

6. str_replace(string_to_be_replaced,string_replaced_with,string).

This function is used to replaced a part of text with some text. It takes three parameters, first parameter which we want to replace, second parameter is a new string which we want to include in a given string and last parameter is a variable itself.

<?php
echo str_replace('World','PHP','Hello World');
?>

Output : Hello PHP.

7.ucwords($str).

This function is used to convert first character of every word into uppercase.

<?php
echo ucwords('hello world');
?>

Output : Hello World.

8. strtoupper($str).

This function is used to convert whole string into uppercase.

<?php
echo strtoupper('hello world');
?>

Output : HELLO WORLD.

9. strtolower($str).

This function is used to convert whole string into lowercase.

<?php
echo strtolower('HELLO WORLD');

?>

Output : hello world.

10. str_repeat($str,’no_of_repetition’).

This function is used to repeat a given string into given number of times. The first parameter is a string and second parameter is a number of times we want to repeat.

<?php
echo str_repeat('hello',5);
?>

Output : hellohellohellohellohello.

11. substr($str,$start,$length).

This function is used to extract a string from a particular position.

<?php
echo substr('hello world',0,5);
?>

Output : hello.

Related Articles

Leave a Reply

Back to top button