douan6815 2012-03-11 10:20
浏览 41

将特殊字符写入txt文件

I am reading some data from a remote file, got every thing working till the point when i write some specific lines to a text file.
problem here is, when i write something like Girl's Goldtone 'X' CZ Ring it becomes Girl & apos;s Goldtone &apos ;X & apos; CZ Ring in txt file.
how do i write to txt file so that it retains text like written above and not show character code but actual character.
sample of my code.

$content_to_write = '<li class="category-top"><span class="top-span"><a class="category-top" href="'.$linktext.'.html">'.$productName.'</a></span></li>'."
";
                fwrite($fp, $content_to_write);  
$linktext = "Girls-Goldtone-X-CZ-Ring";  
$productName = "Girl's Goldtone 'X' CZ Ring";  

var_dump

string '<li class="category-top"><span class="top-span"><a class="category-top" href="Stellar-Steed-Gallery-wrapped-Canvas-Art.html">&apos;Stellar Steed&apos; Gallery-wrapped Canvas Art</a></span></li>

' (length=195)

Code

                $productName =$linktext;


                $linktext = str_replace(" ", "-", $linktext);
                $delChar = substr($linktext, -1);
                if($delChar == '.')
                {
                    $linktext = substr($linktext, 0, -1);
                }

                $linktext = removeRepeated($linktext);
                $linktext = remove_invalid_char($linktext);
                $productName = html_entity_decode($productName);
                $content_to_write = '<li class="category-top"><span class="top-span"><a class="category-top" href="'.$linktext.'.html">'.$productName.'</a></span></li>'."
";
                var_dump($content_to_write);
                fwrite($fp, utf8_encode($content_to_write));
  • 写回答

2条回答 默认 最新

  • duan7772 2012-03-11 10:34
    关注

    Is it that you are reading the data from a remote file and then writing the same to a txt file? Agree with the above comment, its an issue with encoding. Try the following code:

    $file = file_get_contents("messages.txt");
    $file = mb_convert_encoding($file, 'HTML-ENTITIES', "UTF-8");
    echo $file;
    

    echo the response to your browser and see. If found proper, write the response to your txt file. Ensure that your txt file is UTF8 - encoded.

    Check this out:: Write Special characters in a file.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。