php - Removing a span with a specific class from HTML , but not the content using regular expression -
here sample html
<div> <span class="target"> remove parent span class , save text </span> </div>
here want above html following using regex function only
<div> remove parent span class , save text </div>
i have tried this:
$html = preg_replace('#<h3 class="target>(.*?)</h3>#', '', $html);
but didn't work.
you matching wrong tag, h3 instead of span check signature of preg_replace, 2nd argument replacement, in case empty string.
$html = preg_replace('/<(span)[^\>]+>(.*?)<\/\1>/i', '\2', $html);
edit: noticed op wanted remove spans specific class
$html = preg_replace('/<(span).*?class="\s*(?:.*\s)?target(?:\s[^"]+)?\s*"[^\>]*>(.*)<\/\1>/i', '\2', $html);
this should cover spans number of attributes , classes , replace spans has class target.
Comments
Post a Comment