Practical No 4 (WBP) 04
Practical No 4 (WBP) 04
Roll no: 04
Batch: C1
Practical no: 4
Output:
Q2. Write a PHP program to count the number of words in string without using string
functions.
<html>
<head>
<title>Hello</title>
</head>
<body>
<?php
function get_str_num_count($string)
{
$string=preg_replace('/\s+/',' ',trim($string));
$words=explode(" ",$string);
return count($words);
}
$str="jaihind Polytechnic";
$len=get_str_num_count($str);
echo"Given string: ".$str;
echo"<br>no of words is: ",$len;
?>
</body>
</html>
Output:
Q3. Write a simple PHP program to demonstrate use of various built-in string function.
<html>
<head>
<title>String Function</title>
</head>
<body>
<?php
$string = "Hello, World! This is a PHP string functions example.";
$str="PHP";
echo"String length: <br>";
$length = strlen($string);
echo "String Length: $length<br>";
echo"ucwords: <br>";
$ucwords = ucwords($string); // Get first 10 characters
echo "ucwords: $ucwords<br>";
echo"Replace:<br>";
$replace = str_replace('PHP', 'JavaScript', $string);
echo "Replace: $replace<br>";
echo"Comparision: <br>";
$strcmp = strcmp($string,$str);
echo "Compared: $strcmp<br>";
echo"Reverse: <br>";
$reversed = strrev($string);
echo "Reversed: $reversed<br>";
?>
</body>
</html>
Output: