What is the best practice for escaping text received through $_POST in PHP?

When receiving user input through $_POST in PHP, it is important to escape the text to prevent potential security vulnerabilities such as SQL injection or cross-site scripting attacks. The best practice for escaping text received through $_POST is to use the htmlentities() function to convert special characters to HTML entities. This function will ensure that any potentially harmful characters are rendered harmless when displayed on a webpage.

$input_text = $_POST['input_text']; // Assuming 'input_text' is the name of the input field

$escaped_text = htmlentities($input_text);

// Now $escaped_text can be safely used in your application