特殊字符编码textareas

时间:2015-03-16 06:59:43

标签: javascript php html

我将文本文件中的内容拉到要使用的textarea中,并注意到斜杠出现在引号和撇号附近。我能够通过禁用服务器上的魔术引号来解决这个问题,但我注意到特殊字符似乎仍然无法正常显示。

我想弄清楚的是,有什么方法可以检索文件以正确解码/编码它们或对它们进行编码,这样它们首先符合UTF 8标准吗?下面是我检索文件的编码:

<?php
    $directory = $directory = 'users/' . $_SESSION['username'];
    $filesContents = Array();
    $files = scandir( $directory ) ;

    foreach( $files as $file ) {
        if ( ! is_dir( $file ) ) {
            $filesContents[$file] = file_get_contents($directory , $file);
            echo '<option value="'. $file .'">' . $file . '</option>';
        }
    }
?>
</select>

以下是我的保存脚本:

if($_POST['Action'] == "SAVE") {
    //  If a session already exists, this doesn't have any effect.
    session_start();

    //  Sets the current directory to the directory this script is running in
    chdir(dirname(__FILE__));

    //  Breakpoint
    if( empty($_SESSION['username']) || $_SESSION['username'] == '' ) {
        echo 'There is no session username';
    }
    if( empty($_POST['CodeDescription']) || $_POST['CodeDescription'] == '' ) {
        echo 'There is no POST desired filename';
    }

    //  This is assuming we are working from the current directory that is running this PHP file.
    $USER_DIRECTORY = 'users/'.$_SESSION['username'];

    //  Makes the directory if it doesn't exist
    if(!is_dir($USER_DIRECTORY)):
        mkdir($USER_DIRECTORY);
    endif;

    //  Put together the full path of the file we want to create
    $FILENAME = $USER_DIRECTORY.'/'.$_POST['CodeDescription'].'.txt';
    if( !is_file( $FILENAME ) ):
        // Open the text file, write the contents, and close it.
        file_put_contents($FILENAME, $_POST['Code']);
    endif;
    header('Location: mysite.site/evo/codesaveindex.php?saved=1&file='.$FILENAME);
}
?>

0 个答案:

没有答案