Practical No.
5
1. Write a program to develop string functions.
<?php
$str="hello php";
$arr=array("blue","red","pink");
echo "strlen function: ";
echo strlen("Hello World");
echo "<br>";
echo "str_word_count function: ";
echo str_word_count("hello world");
echo "<br>";
echo "strrev function: ";
echo strrev("hello world");
echo "<br>";
echo "strpos function: ";
echo strpos($str,"php");
echo "<br>";
echo "strreplace function: ";
print_r (str_replace("red","yellow",$arr));
echo "<br>";
echo "strcmp function: ";
echo strcmp("hello world","hello world");
echo "<br>";
echo "strtolower function: ";
echo strtolower("HELLO");
echo "<br>";
echo "strtoupper function: ";
echo strtoupper("hello");
echo "<br>";
echo "ucwords function: ";
echo ucwords("welcome to php programming");
?>
Output
2. Write a program to calculate length of a string.
<?php
$str="hello php";
$arr=array("blue","red","pink");
echo "strlen function: ";
echo strlen("Hello World");
?>
Output:
3. Write a program to count the number of words in string without using string functions
<?php
$str="Hello to PHP Programming";
$words= explode(" ", $str);
echo "Number of words: ".count($words);
?>
Output: