Don Alexander Eckford's String Manipulation Demo
Don Alexander Eckford's String Manipulation Form
CODE FOLLOWS
<?php
include('includes/header.php');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Don Alexander Eckford's String Manipulation Demo</title>
<style>
body {
font-family: Arial, sans-serif;
}
h1 {
color: #333;
}
p {
background-color: #f9f9f9;
border: 1px solid #ccc;
padding: 10px;
border-radius: 5px;
}
</style>
</head>
<body>
<h1>Don Alexander Eckford's String Manipulation Form</h1>
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST">
<label for="userString">Enter a String:</label>
<input type="text" id="userString" name="userString" required>
<br><br>
<label for="wordToReplace">Word to Replace:</label>
<input type="text" id="wordToReplace" name="wordToReplace">
<br><br>
<label for="replacementWord">Replace With:</label>
<input type="text" id="replacementWord" name="replacementWord">
<br><br>
<label for="substringToFind">Find Substring:</label>
<input type="text" id="substringToFind" name="substringToFind">
<br><br>
<label for="caseSensitive">Case Sensitive Search:</label>
<input type="checkbox" id="caseSensitive" name="caseSensitive" value="1">
<br><br>
<button type="submit">Submit</button>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$userString = $_POST['userString'];
$wordToReplace = $_POST['wordToReplace'];
$replacementWord = $_POST['replacementWord'];
$substringToFind = $_POST['substringToFind'];
$caseSensitive = isset($_POST['caseSensitive']);
echo "<h1>String Manipulations for: \"$userString\"</h1>";
// 1. String Length
$length = strlen($userString);
echo "<p><strong>Length of the string:</strong> $length characters</p>";
// 2. Substring (first 5 characters)
$substring = substr($userString, 0, 5);
echo "<p><strong>First 5 characters:</strong> $substring</p>";
// 3. Convert to Uppercase
$uppercase = strtoupper($userString);
echo "<p><strong>Uppercase:</strong> $uppercase</p>";
// 4. Convert to Lowercase
$lowercase = strtolower($userString);
echo "<p><strong>Lowercase:</strong> $lowercase</p>";
// 5. Reverse the string
$reversed = strrev($userString);
echo "<p><strong>Reversed string:</strong> $reversed</p>";
// 6. Word count
$wordCount = str_word_count($userString);
echo "<p><strong>Word count:</strong> $wordCount</p>";
// 7. Replace word based on user input
if (!empty($wordToReplace) && !empty($replacementWord)) {
$replaced = str_replace($wordToReplace, $replacementWord, $userString);
echo "<p><strong>Replaced '$wordToReplace' with '$replacementWord':</strong> $replaced</p>";
} else {
echo "<p>No replacement was performed because the word to replace or the replacement word was missing.</p>";
}
// 8. Find position of substring based on user input and case sensitivity
if (!empty($substringToFind)) {
if ($caseSensitive) {
$pos = strpos($userString, $substringToFind);
} else {
$pos = stripos($userString, $substringToFind); // Case-insensitive search
}
if ($pos !== false) {
echo "<p><strong>Position of '$substringToFind':</strong> $pos</p>";
} else {
echo "<p><strong>'$substringToFind'</strong> not found in the string.</p>";
}
} else {
echo "<p>No substring search was performed because the substring to find was missing.</p>";
}
// After processing the form, redirect to clear the data
header("Location: " . $_SERVER['PHP_SELF']);
}
include('includes/footer.php');
?>