1

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?

john
  • 1,263
  • 5
  • 18
  • The manual is kind of vague with `all applicable characters`. All characters have an entity, I believe the function is meant to only convert characters outside of ASCII charset, or special characters `&<>'"`. A space doesn't meet that. If you wanted to convert all characters the `test` would also convert. e.g. ` test` could be ` test` – user3783243 Dec 09 '20 at 13:23
  • 1
    Unrelated but `action=""` does nothing and can be removed. If you fixed it with an `echo` you'd be open to XSS injections. – user3783243 Dec 09 '20 at 13:33

1 Answers1

1

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

Giso Stallenberg
  • 942
  • 7
  • 10