How to strip html tags with php on textarea?

Member

by ryleigh , in category: PHP General , 4 months ago

How to strip html tags with php on textarea?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by mac , 4 months ago

@ryleigh 

To strip HTML tags from text inputted in a textarea using PHP, you can use the strip_tags() function. Here is an example code snippet to demonstrate how to use strip_tags() on textarea input:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $textarea_input = $_POST["textarea_input"];
    
    // Strip HTML tags from the textarea input
    $cleaned_input = strip_tags($textarea_input);
    
    // Output the cleaned input
    echo $cleaned_input;
}
?>

<form method="post">
    <textarea name="textarea_input"></textarea>
    <input type="submit" value="Submit">
</form>


In this code snippet, when the form is submitted, the input from the textarea is stored in the variable $textarea_input. The strip_tags() function is then used to remove any HTML tags from the input, and the cleaned input is stored in the variable $cleaned_input. Finally, the cleaned input is echoed back to the user.


Please note that strip_tags() will remove all HTML tags from the input, so if you want to allow certain tags or attributes, you can provide a second parameter to strip_tags() with a list of allowed tags.