dongluan5740 2015-11-02 08:57
浏览 53

php在变量中写入双引号的文件

I'm having problems when I try to write doble quotes from a variable into a text file. When a piece of text comes with a " text " the file is not created. I tried several things but nothing seems to work. By now I have to avoid write double quotes.

Here is my code in PHP :

<?php 

  $data = json_decode(stripslashes($_POST['data']));
  $name = json_decode(stripslashes($_POST['name']));
  $url_to = json_decode(stripslashes($_POST['url_to']));
  $saveToFile = $url_to . $name . '.srt';

  $space = "
";
  $blank = " ";
  $arrow = " --> ";

  if (isset($data))
  {

  $h = fopen($saveToFile, 'w+');
  if ($h) {
    foreach ($data as $sub) {
        fwrite($h,$sub->number);
        fwrite($h,$space);
        fwrite($h,$sub->start);
        fwrite($h,$arrow);
        fwrite($h,$sub->end);
        fwrite($h,$space);
        if(strpos($sub->text,'"')!==false){
            $sub->text = str_replace('"',"'",$sub->text);
            fwrite($h,$sub->text);
        } else {
            fwrite($h,$sub->text);
        }
        fwrite($h,$space);
        fwrite($h,$space);
    }
    fclose($h);
  }
  exit('Data Saved.');
 }
?>

To clarify some things: I get the text from a "stringified" JSON variable coming from a HTML5 front end page, and I write this text to a file, but if it comes with a double quote, nothing is written.

thanks

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么