0

我的问题是:我正在尝试阅读 *. php 中的 RTF 将值添加到我在 mysql 中的数据库中,一切都很顺利,直到我打开生成的文件并且我看到没有特殊字符让我很好。这是我的代码,抱歉,这是我的第一篇文章,我不知道如何正确设置它:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title></title>
    </head>
    <body>

<?php
ini_set("default_charset", "utf-8");
mysql_query("SET NAMES 'utf8'");


// Read rtf
$plantilla = file_get_contents('plantilla_titulo.rtf');


// Agregamos los escapes necesarios
$plantilla = addslashes($plantilla);
$plantilla = str_replace('\r','\\r',$plantilla);
$plantilla = str_replace('\t','\\t',$plantilla);

// Data in php to rtf
$nombre = $_POST["nombre"];
$curso = $_POST["curso"];
$fechahoy = date("d-m-Y", time());

//Here I Replace who  shows me for special characters, 
//but it doesn't works, now show me characters like " or +- ...
$plantilla = str_replace("\'d3",'Ó',$plantilla);
$plantilla = str_replace("\'f1",'ñ',$plantilla);
$plantilla = str_replace("\'f3",'ó',$plantilla);
$plantilla = str_replace("\'ed",'í',$plantilla);
$plantilla=utf8_encode($plantilla);


// Procces rtf
eval( '$rtf = <<<EOF_RTF
' . $plantilla . '
EOF_RTF;
' );

//Here i do a var_dump($rtf); and show me special characters. 

// Save rtf
file_put_contents("$nombre-$fechahoy.rtf",$rtf);


echo "<a href=\"$nombre-$fechahoy.rtf\">download</a>";

?>

    </body>
</html>

我做了一个手动“调试”,我的问题就在这行,因为我正在做 var_dump($rtf); 把它给我看吧……希望你能帮助我,谢谢!

4

1 回答 1

0

首先,decode,然后replace

$plantilla = utf8_decode($plantilla);
$plantilla = str_replace("\'e1",'á',$plantilla);
$plantilla = str_replace("\'c1",'Á',$plantilla);
$plantilla = str_replace("\'e9",'é',$plantilla);
$plantilla = str_replace("\'c9",'É',$plantilla); 
etc...
// Save rtf, here decode again
$rtf = utf8_decode($rtf);

file_put_contents("$nombre-$fechahoy.rtf",$rtf);
于 2015-04-10T18:53:07.087 回答