String Operations



On Day 2, we learned how to use strings. Today we will learn more string related operations.

PHP provide various functions that help us to work with strings.

Finding Length of a String

strlen() function is used to find length of a string.

strlen.php

PHP Code:
<?php

$companyName 
'FlashWebHost.com';

echo 
strlen($companyName);
Convert Text to Upper Case.

PHP Provide strtoupper() function to convert strings (text) to upper case.

strtoupper.php

PHP Code:
<?php

$companyName 
'FlashWebHost.com';

echo 
strtoupper($companyName);
Convert Text to Lower Case.

strtolower() function can convert text to lower case.

strtolower.php

PHP Code:
<?php

$companyName 
'FlashWebHost.com';

echo 
strtolower($companyName);
Convert First Letter of Words to Upper Case

ucwords() convert first letter of each word to upper case.

ucwords.php

PHP Code:
<?php

$myCar 
'I have a red car';

$myCar ucwords($myCar);

echo 
$myCar;

echo 
'<br>';

$myBus 'I HAVE A RED BUS';

$myBus ucwords($myBus);

echo 
$myBus;
Replacing Text

str_replace() function in allow you to replace text in a string.

str_replace.php

PHP Code:
<?php

$myCar 
'I have a red car';

$myCar str_replace('red''white'$myCar);

echo 
$myCar;

Here str_replace() find text 'red' and replace it with 'white' inside variable $myCar.