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...
精彩评论