How to replace all instances that has letter "s" with number "5"

I created a form that the user can type first and last name. The result should be replace letter "s" with "5".

The form page code:

<form action="form.php" method="post">
First Name: <input type="text" name="firstname"><br>
Last Name: <input type="text" name="lastname"><br>
<input type="submit">
</form>

The result page code:

<?php 
    echo $_POST= str_replace("s","5",$_POST); 
?>

1 answer

  • answered 2018-02-13 02:13 1000Nettles

    You're almost there, try the following:

    <?php
         $_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
         $fullname = $_POST['firstname'] . ' ' . $_POST['lastname'];
         echo str_replace("s", "5", $fullname);
    ?>
    

    Note that we have sanitized your $_POST array in case the user has input malicious values which could cause XSS attacks. See this answer for more details: https://stackoverflow.com/a/4861211/823549