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 CSS通配符清除内外边距为什么可以覆盖默认样式?
  • ¥15 SPSS分类模型实训题步骤
  • ¥15 求解决扩散模型代码问题
  • ¥15 工创大赛太阳能电动车项目零基础要学什么
  • ¥20 limma多组间分析最终p值只有一个
  • ¥15 nopCommerce开发问题
  • ¥15 torch.multiprocessing.spawn.ProcessExitedException: process 1 terminated with signal SIGKILL
  • ¥15 QuartusⅡ15.0编译项目后,output_files中的.jdi、.sld、.sof不更新怎么解决
  • ¥15 pycharm输出和导师的一样,但是标红
  • ¥15 想问问富文本拿到的html怎么转成docx的