2

In one of my application i need to strip out or remove a particular tag inside all html tag attributes like :

 <div<del>class</del>=<del>"example"</del>>

I want to remove all these <del> tag which is generated from server side script. I am using the following preg_replace:

  preg_replace("/<.*?>/", "", $string);

But it is replacing all the tags and i only want to replace the tag within html tags. I dont want to remove all <del> tags. I only want to remove those <del> tags which are appearing inside html tags.

4

3 回答 3

1

你可以这样做:

preg_replace('/(?>(?><|\G(?<!^))[^<>]++\K|\G(?<!^))<[^>]++>/', '', '<div class=<some_tag>"example"</some_tag>>');

图案细节:

(?>            # non capturing group (atomic)
    (?>
        <|\G(?<!^)   # < or a contigous match
    )
    [^<>]++\K  # common content of the good tag until a bracket (\K reset the match)
  |            # OR
    \G(?<!^)   # a contiguous match not at the start of the string
)              # close the non capturing group
<[^>]++>       # the ugly tag to remove
于 2013-10-03T04:28:27.113 回答
1

使用正则表达式:

<[^<>]+>

哪里[^<>]+是匹配所有字符的否定类,除了<>.

正则表达式101演示

但是,如果您有 html 标签,但里面没有这些标签,它也会替换 html 标签。

如果是这样,你可以试试这个正则表达式:

(?<==|")<[^<>]+>

编辑:如果您的问题是具体的,您应该更具体地提出您的问题。

只需使用正则表达式替换:

<\/?del>
于 2013-10-03T04:16:26.043 回答
0

您可以使用此功能

strip_tags('<div<del>class</del>=<del>"example"</del>>', '<del>');

如果您使用 strip_tags函数,您将获得以下输出

'<div class="example">'

祝你好运...

于 2013-10-29T08:46:18.703 回答