How to strip html tags with php on textarea?

Member

by ryleigh , in category: PHP General , a year ago

How to strip html tags with php on textarea?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by mac , a year 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.

Related Threads:

How to strip invalid characters from a string in php?
How to remove html tags from an html string using regex?
How to make the text bold between html tags?
How to use flash message with html tags in laravel?
How to get the closing </html> in html dom parser with php?
How to display both xml and html in php?