Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Oaish Qazi 1|Page

Practical No 5:

Q. Write a Program to Count the number of words in a String. (Execute for all return parameter
values).
CODE:
<html>
<head>
<title>Oaish Qazi - 210455</title>
<?php
$str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
echo "String: ".$str;
echo "<br>String Word Count: ".str_word_count($str, 0);
echo "<br>String Word Count: ".json_encode(str_word_count($str, 1));
echo "<br>String Word Count: ".json_encode(str_word_count($str, 2));
?>
</head>
</html>

OUTPUT:

Q. Write a Program to Count the length of a String, to find the position of String and to compare two
Strings.
CODE:
<html>
<head>
<title>Oaish Qazi - 210455</title>
<?php
$str = "Hello World!";
$key = "World";
echo "Length of String:".strlen($str);
echo "<br>";
echo "Position of key in String:".strpos($str, $key);
echo "<br>";
echo "Compare two String ('Hello World!', 'World'):".strcmp($str, $key);
?>
</head>
</html>

OUTPUT:
Oaish Qazi 2|Page

Q. Write a Program to Reverse a String.


CODE:
<html>
<head>
<title>Oaish Qazi - 210455</title>
<?php
$str = "Hello World!";
echo "String: ".$str."<br>";
echo "Reversed String: ".strrev($str);
?>
</head>
</html>

OUTPUT:

Q. Write a Program to Replace a String with the other String. Also count the number of
replacements.
CODE:
<html>
<head>
<title>Oaish Qazi - 210455</title>
<?php
$str = "Hello World!";
echo "String: ".$str."<br>";
$str = str_replace("World", "PHP", $str, $count);
echo "New String: ".$str."<br>";
Oaish Qazi 3|Page

echo "Number of replacements: ".$count."<br>";


?>
</head>
</html>

OUTPUT:

Q. Write a Program to convert the String to Uppercase, Lowercase, capitalize the first character of a
string.
CODE:
<html>
<head>
<title>Oaish Qazi - 210455</title>
<?php
$str = "oAiSh QaZi";
echo "String: ".$str."<br>";

echo "Uppercase: ".strtoupper($str)."<br>";


echo "Lowercase: ".strtolower($str)."<br>";
echo "Capitalize: ".ucfirst($str)."<br>";
?>
</head>
</html>

OUTPUT:

You might also like