String Functions in PHP


Prev Tutorial Next Tutorial

String Functions in PHP

PHP have lots of predefined function which is used to perform operation with string some functions are strlen(), strrev(), strpos() etc.

PHP strlen() function

strlen() function returns the length of a string. In below example strlen() function is used to return length of "Hello world!"

strlen() example in php

<?php

echo strlen("Hello world!");

?>

Output

12

PHP str_word_count() function

str_word_count() function is used to count numbers of words in given string.

str_word_count() example in php

<?php

echo str_word_count("Hello world!");

?>

Output

2

PHP strrev() function

strrev() function is used to revers any string.

strrev() example in php

<?php

echo strrev("Hello world!");

?>

Output

!dlrow olleH

PHP strpos() function

strpos() function is used to search specific position of any words in given string.

strpos() example in php

<?php

echo strpos("Hello world!", "world");

?>

Output

6

PHP str_replace() function

str_replace function is used to replaces some characters with some other characters in a string. In below example we replace world with Faiz.

str_replace() example in php

<?php

echo str_replace("world", "Faiz", "Hello world!");

?>

PHP strtolower() function

strtolower() function is used to convert uppercase latter into lowercase latter.

strtolower() example in php

<?php

$str="Hello friends i am HITESH";  
$str=strtolower($str);  
echo $str;  

?>

Output

hello friends i am hitesh

PHP strtoupper() function

PHP strtoupper() function is used to convert lowercase latter into uppercase latter.

strtoupper() example in php

<?php

$str="Hello friends i AM Hitesh";  
$str=strtoupper($str);  
echo $str;  

?>

Output

HELLO FRIENDS I AM HITESH

Output

Hello Faiz!

PHP ucwords() function

ucwords() function is used to convert first latter of every word into upper case.

ucwords() example in php

<?php

$str="hello friends i am hitesh";  
$str=ucwords($str);  
echo $str;  

?>

Output

Hello Friends I Am Hitesh

PHP ucfirst() function

ucfirst() function returns string converting first character into uppercase. It doesn't change the case of other characters.

ucfirst() example in php

<?php

Hello friends I am HITESH  

?>

Output

Hello friends I am HITESH

PHP lcfirst() function

PHP lcfirst() function is used to convert first character into lowercase. It doesn't change the case of other characters.

lcfirst() example in php

<?php

hello friends I am HITESH  

?>

Output

hello friends I am HITESH

Prev Tutorial Next Tutorial

Google Advertisements
Download Projects

Google Adsense Advertisements

Buy 3 Year Old Domain, Tutorialday.com, SeosmoTools.com @$200

Buy This Ad Space @$50 per Month, Ad Size 300X250 Contact on: hitesh.xc@gmail.com WhatsApp no. 8882538826

Yahoo Advertisements