PHP Practical 5
PHP Practical 5
AIM: Write a program for manipulating Strings and using String Functions
THEORY:
In PHP, strings are sequences of characters enclosed within single, double, or backticks.
They are a fundamental data type and can be manipulated using a wide range of built-in
functions. These functions allow you to perform various operations, such as finding the
length of a string, concatenating strings, converting case (uppercase, lowercase),
extracting substrings, comparing strings, finding and replacing substrings, removing
whitespace, splitting strings into arrays, and joining array elements into a single string.
PROGRAM 1:
AIM: a) Calculate the length of the string
b) Count the number of words in string -without using string function
THEORY:
To determine the length of a string without using built-in functions, we can iterate through
each character in the string and increment a counter for each character encountered. To
count words, we can iterate through the string and increment a counter each time a space
is encountered. Since words are typically separated by spaces, the number of words is
generally one more than the number of spaces. These approaches demonstrate how to
perform basic string operations using fundamental programming concepts and without
relying on pre-built string functions.
CODE A):
<?php
$str1 = "Rupesh Kumar Shukla";
echo "Original string = ",$str1;
echo "<br>";
echo "Lenght of the string = ",strlen($str1);
?>
OUTPUT:
CODE B): Using user define function
<?php
function counting_words() {
$text = "Life is fight between you and what you want. The moment you stop fighting for
what you want, what you dont want will automatically take over";
$word_count = 1;
for ($i = 0; $i < strlen($text); $i++) {
if ($text[$i] === ' ') {
$word_count++;
}
}
echo "Actual String : $text <br>";
return $word_count;
}
$word_count = counting_words();
echo "Number of words: $word_count";
?>
OUTPUT:
THEORY:
PHP program demonstrates the use of various built-in string functions. It starts by
defining a sample string. Then, it utilizes functions like strlen() to determine the string's
length, strtoupper() and strtolower() to convert the string to uppercase and lowercase,
respectively. It also demonstrates string concatenation using the dot operator (.),
extracting a substring using substr(), finding the position of a substring within the string
using strpos(), and replacing a substring with another using str_replace(). This example
showcases the versatility of PHP's string functions in manipulating and working with
textual data effectively.
CODE:
<?php
$str1 = "Bhutkun Kumar Chaturvedi";
echo "Original string = ",$str1;
echo "<br> <br>";
echo "Lenght of the string = ",strlen($str1);echo "<br>";
echo "Reversing the string = ",strrev($str1);echo "<br>";
echo "Changing the string into uppercase = ",strtoupper($str1);echo "<br>";
echo "Changing the string into lowercase= ",strtolower($str1);echo "<br>";
echo "Counting the words in the string = ",str_word_count($str1);echo "<br>";
echo "Changing the first word to uppercase= ",ucwords($str1);
?>
OUTPUT: