Javascript 文本区域中的特殊字符编码

Javascript 文本区域中的特殊字符编码,javascript,php,html,Javascript,Php,Html,我正在将文本文件中的内容拉入一个文本区域以供使用,并注意到斜杠似乎出现在引号和撇号附近。我可以通过在服务器上禁用魔法引号来解决这个问题,但是我注意到特殊字符似乎仍然不能正确显示 我想弄明白的是,在检索文件时,有没有一种方法可以正确地对它们进行解码/编码,或者对它们进行编码,使它们首先符合UTF 8标准?下面是我检索文件的代码: <?php $directory = $directory = 'users/' . $_SESSION['username']; $filesCo

我正在将文本文件中的内容拉入一个文本区域以供使用,并注意到斜杠似乎出现在引号和撇号附近。我可以通过在服务器上禁用魔法引号来解决这个问题,但是我注意到特殊字符似乎仍然不能正确显示

我想弄明白的是,在检索文件时,有没有一种方法可以正确地对它们进行解码/编码,或者对它们进行编码,使它们首先符合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>

你的
中有
吗?只是仔细检查了我的所有页面,是的。有一页缺了,但什么也没改。我唯一没有它的地方是在我的php脚本保存文件中?为了更好地衡量,您可以对发布的代码使用utf8_encode()。这样,整个链都是utf8,您可以控制它,甚至可以在php文件头中的任何内容之前添加它(content-Type:text/html;charset=utf-8);看看这是否解决了什么问题……谢谢Michael,我在表单中添加了accept charset=“UTF-8”,似乎可以解决问题,但只针对一个文件。为了澄清一些事情,我应该将代码字符串utf8_encode()放在哪里。我想弄清楚这一点,还想进一步澄清一下,您的意思是用我的php处理代码将meta标记放在页面上吗?或者调用它的页面?
file\u put\u contents($FILENAME,utf8\u encode($\u POST['code'])
和meta标记应该位于向用户输出HTML的任何页面上。最好的选择是
标题('Content-Type:text/html;charset=utf-8')在会话开始后的php文件的最开始处。这样,所有的通信都将是utf,而hte meta标记是为那些由于某种原因(或者如果您使用的是windows apache:S)而错过utf的浏览器而设计的
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);
}
?>