Issue
Need a regex for preg_replace.
This question wasn't answered in "another question" because not all tags I want to remove aren't empty.
I have not only to remove empty tags from an HTML structure, but also tags containing line breaks as well as white spaces and/or their html code.
Possible Codes are:
<br />              
BEFORE removing matching tags:
<div> 
  <h1>This is a html structure.</h1> 
  <p>This is not empty.</p> 
  <p></p> 
  <p><br /></p>
  <p> <br /> &;thinsp;</p>
  <p> </p> 
  <p>   </p> 
</div>
AFTER removing matching tags:
<div> 
  <h1>This is a html structure.</h1> 
  <p>This is not empty.</p> 
</div>
                        
                        Solution
You can use the following:
<([^>\s]+)[^>]*>(?:\s*(?:<br \/>| | | | | | | )\s*)*<\/\1>
And replace with '' (empty string)
See DEMO
Note: This will also work for empty html tags with attributes.
Answered By - karthik manchala Answer Checked By - Pedro (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.