0% found this document useful (0 votes)
41 views3 pages

Practical No 4 (WBP) 04

This document contains 3 questions and answers regarding string manipulation functions in PHP. Question 1 demonstrates how to calculate the length of a string using strlen(). Question 2 shows how to count the number of words in a string without string functions using a regular expression and explode(). Question 3 provides examples of using various built-in PHP string functions like strtoupper(), strtolower(), ucwords(), str_replace(), strcmp(), str_word_count(), strpos(), and strrev().

Uploaded by

dishabalsaraf
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)
41 views3 pages

Practical No 4 (WBP) 04

This document contains 3 questions and answers regarding string manipulation functions in PHP. Question 1 demonstrates how to calculate the length of a string using strlen(). Question 2 shows how to count the number of words in a string without string functions using a regular expression and explode(). Question 3 provides examples of using various built-in PHP string functions like strtoupper(), strtolower(), ucwords(), str_replace(), strcmp(), str_word_count(), strpos(), and strrev().

Uploaded by

dishabalsaraf
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/ 3

Name: Auti Sai Sunil

Roll no: 04
Batch: C1
Practical no: 4

Q1. Write a PHP program to calculate length of String.


<html>
<head>
<title> String Functions</title>
</head>
<body>
<?php
$str="Welcome to PHP";
$res=strlen($str);
echo"Length of String is ".$res;
?>
</body>
</html>

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"Convert to uppercase: <br>";


$uppercase = strtoupper($string);
echo "Uppercase: $uppercase<br>";

echo"Convert to lowercase: <br>";


$lowercase = strtolower($string);
echo "Lowercase: $lowercase<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"Word Count: <br>";


$wordCount = str_word_count($string);
echo "Word Count: $wordCount<br>";

echo"Position of PHP: <br>";


$position = strpos($string, 'PHP');
echo "Position of 'PHP': $position<br>";

echo"Reverse: <br>";
$reversed = strrev($string);
echo "Reversed: $reversed<br>";
?>
</body>
</html>

Output:

You might also like