You are on page 1of 4

Ex.

no: 9 STRING HANDLING USING PHP


Date: 21.09.2023

Aim:

To write a PHP program to perform string handling operations.

Program:
index.html
<!DOCTYPE html>
<html>
<head>
<title>String Manipulation</title>
</head>
<body>
<h1>String Manipulation Menu</h1>
<form action="index.php" method="post">
<label for="input_string">Enter a string:</label>
<input type="text" name="input_string" id="input_string" required><br><br>
<label for="function">Choose a string function:</label>
<select name="function" id="function">
<option value="strlen">String Length</option>
<option value="toupper">Uppercase</option>
<option value="tolower">Lowercase</option>
<option value="ucfirst">Uppercase First Letter</option>
<option value="ucwords">Uppercase Words</option>
<option value="strrev">Reverse String</option>
<option value="str_replace">Replace Substring</option>
<option value="strpos">Find Substring Position</option>
<option value="substr">Substring</option>
<option value="str_shuffle">Shuffle String</option>
</select><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

index.php
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$input_string = $_POST["input_string"];
$selected_function = $_POST["function"];
switch ($selected_function) {
case "strlen":
$result = strlen($input_string);
break;
case "toupper":
$result = strtoupper($input_string);
break;
case "tolower":
$result = strtolower($input_string);
break;
case "ucfirst":
$result = ucfirst($input_string);
break;
case "ucwords":
$result = ucwords($input_string);
break;
case "strrev":
$result = strrev($input_string);
break;
case "str_replace":
$result = str_replace("search", "replace", $input_string);
break;
case "strpos":
$result = strpos($input_string, "search");
break;
case "substr":
$result = substr($input_string, 2, 5); // Example: Extract a substring starting at
position 2 and having a length of 5
break;
case "str_shuffle":
$result = str_shuffle($input_string);
break;
default:
$result = "Invalid selection";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>String Manipulation Result</title>
</head>
<body>
<h1>String Manipulation Result</h1>
<p>Original String: <?php echo $input_string; ?></p>
<p>Selected Function: <?php echo $selected_function; ?></p>
<p>Result: <?php echo $result; ?></p>
<a href="index.html">Back to Menu</a>
</body>
</html>
Output:

Conduct of Experiment (30)


Record(20)

Viva(10)
Total(60)

Result:
Thus, the PHP program to demonstrate string handling has been done successfully.

You might also like