• <small id='Ds13F'></small><noframes id='Ds13F'>

      1. <legend id='Ds13F'><style id='Ds13F'><dir id='Ds13F'><q id='Ds13F'></q></dir></style></legend>
        <tfoot id='Ds13F'></tfoot>
        <i id='Ds13F'><tr id='Ds13F'><dt id='Ds13F'><q id='Ds13F'><span id='Ds13F'><b id='Ds13F'><form id='Ds13F'><ins id='Ds13F'></ins><ul id='Ds13F'></ul><sub id='Ds13F'></sub></form><legend id='Ds13F'></legend><bdo id='Ds13F'><pre id='Ds13F'><center id='Ds13F'></center></pre></bdo></b><th id='Ds13F'></th></span></q></dt></tr></i><div id='Ds13F'><tfoot id='Ds13F'></tfoot><dl id='Ds13F'><fieldset id='Ds13F'></fieldset></dl></div>
          <bdo id='Ds13F'></bdo><ul id='Ds13F'></ul>

      2. 通过 id 查找元素并用 php 替换其内容

        时间:2023-05-21

        <legend id='itrQo'><style id='itrQo'><dir id='itrQo'><q id='itrQo'></q></dir></style></legend>
        • <bdo id='itrQo'></bdo><ul id='itrQo'></ul>

            <tfoot id='itrQo'></tfoot>

            • <small id='itrQo'></small><noframes id='itrQo'>

                <tbody id='itrQo'></tbody>
                <i id='itrQo'><tr id='itrQo'><dt id='itrQo'><q id='itrQo'><span id='itrQo'><b id='itrQo'><form id='itrQo'><ins id='itrQo'></ins><ul id='itrQo'></ul><sub id='itrQo'></sub></form><legend id='itrQo'></legend><bdo id='itrQo'><pre id='itrQo'><center id='itrQo'></center></pre></bdo></b><th id='itrQo'></th></span></q></dt></tr></i><div id='itrQo'><tfoot id='itrQo'></tfoot><dl id='itrQo'><fieldset id='itrQo'></fieldset></dl></div>

                  本文介绍了通过 id 查找元素并用 php 替换其内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我想使用 PHP 在文件内容中搜索具有特定 ID 的元素,替换其内容,然后将更改保存到文件中.我能够加载 HTML,然后再次将其保存,但是在查找和替换"(目前正在尝试使用 preg_replace)时遇到问题.

                  I want to use PHP to search through the contents of a file for an element with a specific id, replace its contents, then save my changes to the file. I'm able to load in the HTML, and save it back out again, but am having trouble with the 'find and replace' (currently trying to use preg_replace).

                  这是我目前所拥有的:

                  <?php
                  // read in the content
                  $file = file_get_contents('file.php');
                  
                  // parse $file, looking for the id.
                  $replace_with = "id='" . 'myID' . "'>" . $replacement_content . "<";
                  if ($updated = preg_replace('/id="myID">.*?</', $replace_with, $file)) {   
                      // write the contents of $file back to index.php, and then refresh the page.
                      file_put_contents('file.php', $updated);
                  }
                  

                  然而,虽然它成功加载内容并将其写出(我已经通过写入单独的文件对其进行了测试),但似乎 $updated 实际上并没有改变.

                  However, while it successfully loads in the content and writes it out (I've tested it by writing to a separate file), it appears that $updated doesn't actually change.

                  有什么想法吗?

                  推荐答案

                  你可以使用 PHP 的 DOMDocument 为此:

                  You can use PHP's DOMDocument for this:

                  $html = new DOMDocument(); 
                  $html->loadHTMLFile('file.php'); 
                  $html->getElementById('myId')->nodeValue = 'New value';
                  $html->saveHTMLFile("foo.html");
                  

                  这篇关于通过 id 查找元素并用 php 替换其内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:从 PHP 字符串中删除除字母以外的所有内容 下一篇:替换 unicode 字符

                  相关文章

                  最新文章

                  <tfoot id='iRQA7'></tfoot>

                      <i id='iRQA7'><tr id='iRQA7'><dt id='iRQA7'><q id='iRQA7'><span id='iRQA7'><b id='iRQA7'><form id='iRQA7'><ins id='iRQA7'></ins><ul id='iRQA7'></ul><sub id='iRQA7'></sub></form><legend id='iRQA7'></legend><bdo id='iRQA7'><pre id='iRQA7'><center id='iRQA7'></center></pre></bdo></b><th id='iRQA7'></th></span></q></dt></tr></i><div id='iRQA7'><tfoot id='iRQA7'></tfoot><dl id='iRQA7'><fieldset id='iRQA7'></fieldset></dl></div>
                    1. <legend id='iRQA7'><style id='iRQA7'><dir id='iRQA7'><q id='iRQA7'></q></dir></style></legend>
                        <bdo id='iRQA7'></bdo><ul id='iRQA7'></ul>

                      <small id='iRQA7'></small><noframes id='iRQA7'>