我将如何 preg_replace 以下内容?
我有这段代码片段,我将在不同的地方替换它,我想知道如何编写模式来 preg_replace 它?谢谢!
<div class="leftside item1">
<label for="item1">Item1</label>
</div>
我想将其替换为:
<div class="leftside item1">
<label for="item1">Item1</label>
</div>
<div class="rightside item1_select">
<select class="item1_select" id="item1_select">
<option value="">Select one</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
I've got this snippet of code that I will be replacing in various places and I was wondering how would I write the pattern to preg_replace it? Thanks!
<div class="leftside item1">
<label for="item1">Item1</label>
</div>
I'd like to replace it with:
<div class="leftside item1">
<label for="item1">Item1</label>
</div>
<div class="rightside item1_select">
<select class="item1_select" id="item1_select">
<option value="">Select one</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想完全替换它,并且代码不变,请使用
str_replace()
。如果实际字符串有所不同,请不要使用正则表达式,因为它们确实不能与 XML/HTML 或 SGML 很好地混合,请使用解析器(例如,带有 XPath 查询和一些 Nodem 操作的 DOMDocument ) )。If you want to totally replace it, and the code is constant, use
str_replace()
. If the actual string varies somewhat, do NOT use regexes, as they really don't mix well with XML/HTML or SGML, use a parser (DOMDocument
for instance, with an XPath query and some nodem manipulations).