phphtml-entitiesfile-put-contents

how to parse   into txt file with file_put_contents


I am trying to parse a space form an input field in a txt file with file_put_contents but i do not succeed in it with htmlentities.

<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
    if( isset($_POST['translation']) ) {  
        $translation = htmlentities($_POST['translation']);
        
        $translation_input = $translation.PHP_EOL;
        
        $translationfile = 'data/translations.txt';     
        file_put_contents($translationfile, $translation_input);                        
    }
}
?>

<form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST" role="form">                                                                    
    <input class="form-control" name="translation" type="text" value="" />                                                                                                                                                  
    <button type="submit" name="submit" class="btn btn-primary w-100">UPDATE/TRANSLATE</button>                                 
</form>

per example if i first type a space in the input and after the text test my txt file looks like this:

 test

and it should be

&nbsp;test

How can i achieve this?


Solution

  • If it is only about spaces, you could just replace it with str_replace