How can HTML entities like spaces be preserved when transferring form data in PHP?

When transferring form data in PHP, HTML entities like spaces can be preserved by using the htmlentities() function to encode the data before sending it. This function will convert special characters like spaces into their corresponding HTML entities, ensuring that they are not lost during the transfer process.

// Encode form data using htmlentities() before sending
$input_data = $_POST['input_data'];
$encoded_data = htmlentities($input_data);

// Transfer the encoded data to the desired location
// For example, save it to a database or send it in an email