开发者

I want to remove a paragraph which has specific class (PHP/Regex)

开发者 https://www.devze.com 2023-02-01 14:46 出处:网络
I 开发者_开发问答think this won\'t count much combinations please, I need to remove from string a paragraph that has specific class (It is consistent)

I 开发者_开发问答think this won't count much combinations please, I need to remove from string a paragraph that has specific class (It is consistent)

Example:

<p class="special_class">Some content</p>

I want to remove the content of every paragraph which has special_class only. So I would like to run a regex that returns empty. I do not want to use a parser to do this please, i am using this in very little function inside my script.

Thank you :)


It is generally a bad idea to use regex for the HTML parsing. Take a look at Simple HTML DOM Parser instead to find out the specified and remove it.

If you want to use regex anyway, you could trt this instead:

preg_replace('/<p class="special_class">[\w\s]*<\/p>/', '<p class="special_class"></p>');


$result = preg_replace('%<p\s+class="special_class">.*?</p>%s', '', $subject);

should work. It will remove the entire paragraph from the opening to the closing p tag. It expects that the p tag is properly closed. But you already seem to know about the drawbacks of handling HTML with regexes...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号