php怎么修改id元素
- 建站笔记
- 2022-05-18
- 595
php修改id元素的方法:1、创建一个PHP示例文件;2、通过“str_replace("$oldString","$newString","$html");”方式实现替换即可。
本文操作环境:Windows7系统、PHP7.1版、Dell G3电脑。
php怎么修改id元素?
PHP替换字符串中的id元素内容:
如:
$html = '<h1 id="item" class="abc" data="efg">Hello World';
修改方法:
$html = '<h1 id="item" class="abc" data="efg">Hello World'; $elementId ="item"; $newString ="Replace World"; $dom = new DOMDocument(); $dom->loadHTML($html); $belement = $dom->getElementById("$elementId"); $oldString = $belement->nodeValue; $newHTML = str_replace("$oldString","$newString","$html"); echo $newHTML;
注意:请更改$elementId和$newString的原始值
本文由白琉璃于2022-05-18发表在白琉璃源码网,如有侵权或疑问,请联系我们,谢谢。
本文链接:https://www.bailiuli.com/t/2293.html
发表评论