【发布时间】:2015-09-12 16:11:51
【问题描述】:
你好,我有一个这样的 php 正则表达式代码:
preg_replace('~<div\s*.*?(?:\s*class\s*=\s*"(.*?)"|id\s*=\s*"(.*?)\s*)?>~i','<div align="center" class="$1" id="$2">', "html source code");
现在我要做的是替换源 html 代码中的所有标签,然后只保留 div 标签中的类和 id 并添加 align="center" 到它:
示例:<div style="border:none;" class="classbutton"> will be replaced to <div align="center" class="classbutton">
<div style="border:none;" class="classbutton" id="idstyle"> will be replaced to <div align="center" class="classbutton" id="idstyle">
我已经尝试了许多使用 php 正则表达式的代码,但似乎没有什么对我有用。所以如果有人可以帮助我或给我一个 domdocument 代码来解决这个问题。
提前致谢。
【问题讨论】:
-
那是你真正的 HTML 吗?如果是这样,
<和div之间有一个空格。在这里也没有看到domdocument的用法.. -
我正在使用来自网站的 file_get_contents 进行获取
-
然后我替换了网站的所有标签我正在使用如上所述的 php 正则表达式代码,但它不起作用,我听说 domdocument 可以解决我的问题,所以我要求有人给我一个 domdocument 替换我的 php 正则表达式代码跨度>这不是一个只有
gives code的网站。我们帮助解决编码问题。你试过使用domdocument了吗?domdocument是比正则表达式更好的方法。
标签: php regex preg-replace domdocument