douren7921 2013-06-06 19:53
浏览 47
已采纳

覆盖文件中特定div中的文本(PHP HTML)

I am creating a slideshow editor. I have been able to parse a file and present it to the user in a form. Now I need to figure out how to write the saved information to the file. I want the user to be able to edit the information before and after the slideshow, so there is no specific set of information to be able to overwrite the whole file.

If there is a way to get all of the text before the div and copy it to the variable, add the new information, then get the rest of the information after the div and add that to the variable and then write all that information to the file, then that would work. Otherwise, here is what I have put together.

                /* Set Variables */
    $x = $_POST['x'];
    $file = $_POST['file'];
    $path = '../../yardworks/content_pages/' . $file;
    $z=0;

    while ($z<$x){
        $title[$z] = $_POST['image-title'.$z];
        $description[$z] = $_POST['image-desc'.$z];
        $z++;
    }

    for ($y=0; $y<$x; $y++){
        $contents .= '<li>
    <a class="thumb" href="images/garages/'.$file[$y].'">
        <img src="images/garages/'.$file[$y].'" alt="'.$title[$y].'" height="100px" width="130px" class="slideshow-img" />
    </a>
    <div class="caption">
        <div class="image-title">'.$file[$y].'</div>
        <div class="image-desc">'.$description[$y].'</div>
    </div>
</li>';
    }

    /* Create string of contents */
    $mydoc = new DOMDocument('1.0', 'UTF-8');
    $mydoc->loadHTMLFile($path);
    $mydoc->getElementById("replace")->nodeValue = $contents;
    $mydoc->saveHTMLFile($path);

    $file = file_get_contents($path);
    $file = str_replace("&lt;", "<", $file);
    $file = str_replace("&gt;", ">", $file);
    file_put_contents($path, $file);

?>

Nothing throws out an error, but the file also remains unchanged. Is there anything I can change or fix to make it write to the file? This is all I have been able to find regarding this specific problem.

I would like to stick to one language, but if I find a way to write to the file using javascript, do the php variables pass on to the javascript section or do I have to stick with one language?

**Edit Everything is working. ONE problem: is there a way to keep the special characters without converting them? I need the < and > to stay as they are and not convert to a string

  • 写回答

1条回答 默认 最新

  • donljt2606 2013-06-07 14:43
    关注

    I have decided to save the file as it is and use a separate code set to replace the string. I have edited my question above.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao
  • ¥20 Vite 打包的 Vue3 组件库,图标无法显示
  • ¥15 php 同步电商平台多个店铺增量订单和订单状态
  • ¥15 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题