0% found this document useful (0 votes)
2 views

Exp 5

The document contains PHP programs demonstrating various string functions, including counting words, measuring string length, reversing strings, replacing substrings, and changing case. Each program is accompanied by code snippets and outputs, showcasing the functionality of string manipulation in PHP. The author of the document is Midhat Ansari, with roll number 210403.

Uploaded by

ansarisadeem8879
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Exp 5

The document contains PHP programs demonstrating various string functions, including counting words, measuring string length, reversing strings, replacing substrings, and changing case. Each program is accompanied by code snippets and outputs, showcasing the functionality of string manipulation in PHP. The author of the document is Midhat Ansari, with roll number 210403.

Uploaded by

ansarisadeem8879
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Name: Midhat Ansari Roll no: 210403

Sub: Web Based Application Development with PHP (WBP) Branch: CO


Experiment No. 5: Write a PHP program for use of strings functions
1. Write a Program to Count the number of words in a String. (Execute for all return
parameter values).
CODE:
<html>
<body>
<?php
$str="Hello, Good afternoon everyone .";
print_r(str_word_count($str,0));
echo "<br>";
print_r(str_word_count($str,1));
echo "<br>";
print_r(str_word_count($str,2));
echo "<hr>Midhat Ansari-210403";
?>
</body>
</html>

OUTPUT:
2. Write a Program to Count the length of a String, to find the position of String and to
compare two Strings.
CODE:
<html>
<body>
<?php
$str="Hello, Good afternoon";
$str1="Hello";
$str2="hello";
$str3="Evening";
$str4="Evening";
echo "Length of $str= ".strlen($str)."<br>";
echo "Use of strpos():<br>";
echo strpos($str,"good")."<br>";
echo strpos($str,"Good")."<br>";
echo strpos($str,"afternoon",-10)."<br>";
echo "Use of strcmp():<br>";
echo strcmp($str1,$str2)."<br>";
echo strcmp($str2,$str3)."<br>";
echo strcmp($str3,$str4)."<br>";
echo strcmp($str1,$str4)."<br>";
echo "<hr>Midhat Ansari-210403";
?>
</body>
</html>

OUTPUT:
3. Write a Program to Reverse a String.
CODE:
<html>
<body>
<?php
$str="Evening";
echo "Reverse of $str= ".strrev($str)."<br>";
echo "<hr>Midhat Ansari-210403";
?>
</body>
</html>

OUTPUT:
4. Write a Program to Replace a String with the other String. Also count the number of
replacements.
CODE:
<html>
<body>
<?php
$str="Good evening";
echo "Original: ".$str."<br>";
echo str_replace("evening","Morning",$str)."<br>";
$arr=array("blue","red","green","yellow","green");
print_r(str_replace("green","purple",$arr,$r))."<br>";
echo "<br>Replacements: $r"."<br>";
$a=array("blue","red","green","yellow");
$f=array("blue","red","green");
$rep=array("purple","pink");
print_r(str_replace($f,$rep,$a));
echo "<hr>Midhat Ansari-210403";
?>
</body>
</html>

OUTPUT:
5. Write a Program to convert the String to Uppercase, Lowercase, capitalize the first
character of a string.
CODE:
<html>
<body>
<?php
$str="Good evening";
echo "Uppercase of $str= ".strtoupper($str)."<br>";
echo "Lowercase of $str= ".strtolower($str)."<br>";
echo "Use of ucwords()= ".ucwords($str," ")."<br>";
echo "<hr>Midhat Ansari-210403";
?>
</body>
</html>

OUTPUT:

You might also like