In this tutorial we are going to discuss about PHP String and String functions.  Here String is one of the PHP data type.

PHP String :

PHP string is a collection of subsequent characters put together to form a sentence. There are many methods to manipulating a PHP string. Let’s discuss one by one.,

PHP strlen()

The PHP strlen() function is used whenever the length of a string is required.

<?php
echo strlen("Hello My Name is Marry");
?>

Output :

22

Note that spaces are also counted as characters.

PHP str_word_count() :

PHP uses the str_word_count() function to output the number of words present in a string.

<?php
echo str_word_count("Hello My Name is Marry");
?>

Output :

5

PHP strrev() :

This function reverses a string. Written as strrev()

<?php
echo strrev("Hello");
?>

Output :

olleH

PHP strpos() :

PHP strpos() is a function used to search for text within a string. It returns the position of the first character or FALSE for 0 matches.

<?php
echo strpos("My Name is Chandra","Chandra");
?>

Output :

11

PHP str_replace() :

PHP str_replace() function is used to replace parts of a string. In the example below, the name Chandra will be replaced by Venu.

<?php
echo str_replace("Chandra","Venu","My Name is Chandra");
?>

Output :

My Name is Venu

Above are the most used string functions in PHP.

Keep Learning 🙂