如何使用正则表达式删除特定的标签(How to delete a specific tag using regex)

编程入门 行业动态 更新时间:2024-10-15 10:17:41
如何使用正则表达式删除特定的标签(How to delete a specific tag using regex)

我试图删除ul标签和嵌套标签里面的ul。

<ul class="related"> <li><a href="/related-1.html" target="_blank">Related article</a></li> <li><a href="/related-2.html" target="_blank">Related article 2</a></li> </ul>

我刚刚使用这个删除了ul内部的嵌套li(我为这个东西使用了php,所以我把内容从db作为$内容)

$content = $rq['content']; //here is the <ul class="related">... code $content1 = preg_replace('~<ul[^>]*>\K(?:(?!<ul).)*(?=</ul>)~Uis', '', $content); //it works here

到目前为止,我获得了$content1的下一个字符串

<ul class="related"></ul>

那么如何使用正则表达式来删除这段剩余的代码呢? 我尝试了类似的模式,但没有得到我想要的结果。

$finalcontent = preg_replace('~<ul[^>]*>\K.*(?=</ul>)~Uis', '', $content1);

I'm trying to delete a ul tag and nested tags inside that ul.

<ul class="related"> <li><a href="/related-1.html" target="_blank">Related article</a></li> <li><a href="/related-2.html" target="_blank">Related article 2</a></li> </ul>

I just deleted the nested li inside the ul using this (I'm using php for this thing, so I pulled content from a db as $content)

$content = $rq['content']; //here is the <ul class="related">... code $content1 = preg_replace('~<ul[^>]*>\K(?:(?!<ul).)*(?=</ul>)~Uis', '', $content); //it works here

So far I get the next string in $content1

<ul class="related"></ul>

So how do I delete this piece of remaining code using regex? I tried the similar pattern but did not get the results I am wanting.

$finalcontent = preg_replace('~<ul[^>]*>\K.*(?=</ul>)~Uis', '', $content1);

最满意答案

以下内容可能适合您的目的:

$content1 = '<p>Foo</p><ul class="related"></ul><p>Bar</p>'; $finalcontent = preg_replace('~<ul[^>]*>.*</ul>~Uis', '', $content1); echo $finalcontent;

preg_replace调用应该从$content1删除所有<ul...>...</ul> 。 对于给定的示例内容,它返回:

<p>Foo</p><p>Bar</p>

如果您希望替换更具体,例如,为了仅删除<ul class="related">...</ul>但不删除其他类型的<ul>...</ul> ,你可以使正则表达式更具体。 例如:

$content1 = '<p>Foo</p><ul class="related"></ul><p>Bar</p><ul><li>Do not delete this one</li></ul>'; $finalcontent = preg_replace('~<ul class="related">.*</ul>~Uis', '', $content1); echo $finalcontent;

对于给定的例子,这将返回:

<p>Foo</p><p>Bar</p><ul><li>Do not delete this one</li></ul>

The following may suit your purpose:

$content1 = '<p>Foo</p><ul class="related"></ul><p>Bar</p>'; $finalcontent = preg_replace('~<ul[^>]*>.*</ul>~Uis', '', $content1); echo $finalcontent;

The preg_replace call should remove all occurrences of <ul...>...</ul> from $content1. For the given example content, it returns:

<p>Foo</p><p>Bar</p>

If you want the replacement to be more specific, e.g., in order to only remove occurrences of <ul class="related">...</ul> but not other types of <ul>...</ul>, you can make the regex more specific. For example:

$content1 = '<p>Foo</p><ul class="related"></ul><p>Bar</p><ul><li>Do not delete this one</li></ul>'; $finalcontent = preg_replace('~<ul class="related">.*</ul>~Uis', '', $content1); echo $finalcontent;

For the given example, this would return:

<p>Foo</p><p>Bar</p><ul><li>Do not delete this one</li></ul>

更多推荐

本文发布于:2023-07-15 17:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1116668.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   标签   正则表达式   delete   tag

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!