<?php
include('includes/header.php');
echo "<h1>Evans's String Manipulation Demo</h1>";
// Check if form submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Get input strings safely
$str1 = trim($_POST['string1'] ?? '');
$str2 = trim($_POST['string2'] ?? '');
echo "<h2>Original Strings</h2>";
echo "<p>String 1: <strong>" . htmlspecialchars($str1) . "</strong></p>";
echo "<p>String 2: <strong>" . htmlspecialchars($str2) . "</strong></p>";
// Length of strings
echo "<h2>String Lengths</h2>";
echo "<p>Length of String 1: " . strlen($str1) . "</p>";
echo "<p>Length of String 2: " . strlen($str2) . "</p>";
// Substring examples (first 5 chars of string1)
echo "<h2>Substring</h2>";
echo "<p>First 5 characters of String 1: " . substr($str1, 0, 5) . "</p>";
// Position of a character (search for letter 'a' in string2)
$pos_a = strpos($str2, 'a');
echo "<h2>Character Position</h2>";
if ($pos_a !== false) {
echo "<p>First occurrence of 'a' in String 2 is at position: $pos_a</p>";
} else {
echo "<p>The character 'a' was not found in String 2.</p>";
}
// Replace example: replace "hello" with "hi" in string1 (case insensitive)
$replaced = str_ireplace("hello", "hi", $str1);
echo "<h2>Replace Text</h2>";
echo "<p>String 1 after replacing 'hello' with 'hi': $replaced</p>";
// Uppercase / lowercase
echo "<h2>Case Conversion</h2>";
echo "<p>String 1 uppercase: " . strtoupper($str1) . "</p>";
echo "<p>String 2 lowercase: " . strtolower($str2) . "</p>";
} else {
// Show input form
?>
<form method="post" action="">
<label for="string1">Enter String 1:</label><br>
<input type="text" id="string1" name="string1" required><br><br>
<label for="string2">Enter String 2:</label><br>
<input type="text" id="string2" name="string2" required><br><br>
<input type="submit" value="Submit">
</form>
<?php
}
include('includes/footer.php');
?>