Name: Sanket Ilag
Roll No: 63
Practical 5:
A. Write a PHP program to-
•
Calculate length of string.
•
Count the number of words in string without using string
functions. B. Write a simple PHP program to demonstrate use of
various built-in string functions
Code: Length of String
<?php
$str = "Welcome to the world of PHP";
$length = strlen($str);
echo "<br> Length of string : " . $length; ?
>
Output:
Code: No. of Words in String
<?php
$str = "Hello world";
$count = 1; // Start from 1 because words = spaces + 1
for ($i = 0; $i < strlen($str); $i++) { // Use `<` instead of `<=` if
($str[$i] == " ") {
$count++;
}
} echo "No. of Words in String = " .
$count; ?>
Output:
Code: All String Functions
<?php
$string = " string HAS its own predifined FUNCTIONS"; $str = "string has its own
predefined functions"; echo "The length of the string is: " . strlen($str); echo "<br>"; echo
"The Result of comparing string is: " . strcmp($string, $str); echo "<br>"; echo "The
Result of the Repeated string is: " . str_repeat($str, 2); echo "<br>"; echo "The Replaced
string is: " . str_replace("predifined", "built-in", $string); echo "<br>"; echo "The Result of
UpperCase is: " . strtoupper($string); echo "<br>"; echo "The Result of LowerCase string
is: " . strtolower($str); echo "<br>"; echo "The Result of Ltrim string is: " . Ltrim($string);
echo
"<br>";
echo "The Result of Rtrim string is: " . Rtrim($string); echo
"<br>"; echo "The Result of WordCount is: " . str_word_count($str);
echo "<br>"; echo "The Result of Possition is: " . strpos($string, "it's");
echo "<br>"; echo "The Result of Possition is: " . ucwords($str); echo
"<br>"; echo "The Result of Reverse is: " . Strrev($str); echo "<br>";
?>
Output: