問題描述
我想使用 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).
這是我目前所擁有的:
然而,雖然它成功加載內容并將其寫出(我已經通過寫入單獨的文件對其進行了測試),但似乎 $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:
這篇關于通過 id 查找元素并用 php 替換其內容的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!