PHP String Handling Functions

PHP String Functions

In this Tutorial we will look at some commonly used functions to manipulate strings.


Get the Length of string
get the length of the string use the strlen() function in PHP.
example:
<?php
echo strlen("Hello World");    // output:11
?>
========================================================================

Count the numer of words

get the how many words in the string using str_word_count() in PHP.

example:

<?php

echo str_word_count("hello");   //output:5 

?>
=================================================

Replace sting

using str_replace() function replace the string in PHP.

example:

<?php

echo str_replace("world","dolly","Hello world");   //output: Hello dolly 

?>
=================================================

Reverse string

Using strrev() function reverse the string in php.

example:

<?php

echo strrev("hello");   //output:olleH

?>
=================================================

Search for perticular word in string

get the postion of particular word using strpos() in PHP.

example:

<?php

echo strpos("hello world","world");   //output:6

?>
================================================
Previous
Next Post »