How can the length of text input in a textarea be limited in PHP?

To limit the length of text input in a textarea in PHP, you can use the strlen() function to check the length of the input text. If the length exceeds the desired limit, you can display an error message to the user. You can also use the substr() function to truncate the input text to the desired length.

<?php
if(isset($_POST['textarea_input'])){
    $input_text = $_POST['textarea_input'];
    
    if(strlen($input_text) > 100){ // Limit text to 100 characters
        echo "Error: Text input cannot exceed 100 characters.";
    } else {
        $limited_text = substr($input_text, 0, 100); // Truncate text to 100 characters
        echo "Limited text: " . $limited_text;
    }
}
?>

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