首页 > 解决方案 > 如何使用 file_put_contents 解析成 txt 文件

问题描述

我正在尝试使用 txt 文件中的输入字段解析空格,file_put_contents但使用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>

例如,如果我首先在输入中输入一个空格,然后在文本之后,test 我的 txt 文件如下所示:

 test

它应该是

&nbsp;test

我怎样才能做到这一点?

标签: phphtml-entitiesfile-put-contents

解决方案


如果它只是关于空格,你可以把它替换为str_replace


推荐阅读