Jul 6, 2012

String Formatting : length, count and occurrences in PHP


 


Lower to Upper



<?php


$str = "Mary Had A Little Lamb and She LOVED It So";


$str = strtoupper($str);


echo $str; // Print ==> MARY HAD A LITTLE LAMB AND SHE LOVED IT SO


?>


 


Upper to Lower


 


<?php


$str = "Mary Had A Little Lamb and She LOVED It So";


$str = strtolower($str);


echo $str; // Print ==> mary had a little lamb and she loved it so


?>


 


Capitalise First Character


 


<?php


$foo = 'hello world!';


$foo = ucfirst($foo);


echo $foo; // Print ==> Hello world!


?>


 





Capitalise Each Word


 


<?php


$foo = 'hello world!';


$foo = ucwords($foo);


echo $foo; // Print ==> Hello World!


?>


 


String Length


 


<?php


$str = 'abcdef';


echo strlen($str); // Print ==> 6


?>


 


String Count


 


<?php


$text = 'This is a test';


echo substr_count($text, 'is'); // Print ==> 2


echo substr_count($text, 'is', 3, 4); // the text is reduced to 's is', so it print ==> 1


?>


 


String Occurrences


 


<?php


$email  = 'name@example.com';


$domain = strstr($email, '@');


echo $domain; // print ==> @example.com


?>




 

No comments:

Post a Comment