String Manipulation

Steph's String Manipulation

Enter a sentence to see how PHP can manipulate strings.






<?php
include ('../header/assignmentHeader.php');
error_reporting(E_ALL);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>String Manipulation</title>
    <link rel="stylesheet" href="../css/assignmentHeaderStyles.css" />
</head>
<body>

<h1>Steph's String Manipulation</h1>

<p>Enter a sentence to see how PHP can manipulate strings.</p>

<form method="post" action="">
    <label for="text">Enter a sentence:</label>
    <input type="text" id="text" name="text" required>

    <br><br>

    <label for="character">Character to find:</label>
    <input type="text" id="character" name="character"
           maxlength="1" required>

    <br><br>

    <input type="submit" name="submit" value="Submit">
</form>

<?php

if (isset($_POST['submit'])) {

    
$text = $_POST['text'];
    
$character = $_POST['character'];

    echo 
"<h2>String Results</h2>";
    echo 
"<p>Original string: " .
        
htmlspecialchars($text, ENT_QUOTES, 'UTF-8') . "</p>";
    echo 
"<p>Length of string: " . strlen($text) . "</p>";
    echo 
"<p>Uppercase: " .
        
htmlspecialchars(strtoupper($text), ENT_QUOTES, 'UTF-8') . "</p>";
    echo 
"<p>Lowercase: " .
        
htmlspecialchars(strtolower($text), ENT_QUOTES, 'UTF-8') . "</p>";

    
$position = strpos($text, $character);
    if (
$position !== false) {
        echo 
"<p>First occurrence of '" .
            
htmlspecialchars($character, ENT_QUOTES, 'UTF-8') .
            
"' is at position " . $position . "</p>";
    } else {
        echo 
"<p>The character was not found.</p>";
    }

    echo 
"<p>First three characters: " .
        
htmlspecialchars(substr($text, 0, 3), ENT_QUOTES, 'UTF-8') . "</p>";

    
$words = explode(" ", $text);

    echo 
"<h3>Words in an Array</h3>";

    echo 
"<pre>";
    
print_r($words);
    echo 
"</pre>";
}

?>

</body>
</html>

<?php
show_source
(__FILE__);
include(
'../footer/footer.php');
?>