programming-examples/php/Basics/PHP program to find the length of the last word in a string..php

23 lines
540 B
PHP
Raw Permalink Normal View History

2019-11-18 14:44:36 +01:00
<?php
function length_of_last_word($s)
{
if (strlen(trim($s)) == 0)
{
return "Blank String";
}
$words = explode(' ', $s);
if (sizeof($words) >1)
return strlen(substr($s, strrpos($s, ' ') + 1));
else
return "Single word";
}
print_r(length_of_last_word("PHP Exercises")."\n");
print_r(length_of_last_word("PHP")."\n");
print_r(length_of_last_word("")."\n");
print_r(length_of_last_word(" ")."\n");
?>