php strip empty html tags

Posted on 2011-02-14
Last Modified: 2012-05-11
Hi Experts!

With Curl my script receives HTML code from a website. From that source I would like to strip all empty tags such as <p> </p>

Somehow I can't seem to remove them, I think it has something to do with character settings or something. This is what I've tried:

$html = preg_replace('/<[^\/>]*>([\s]?)*<\/[^>]*>/','' , $html);
$html = preg_replace('%<p></p>%','' , $html);
$html = preg_replace('%<p> </p>%','' , $html);
$html = str_replace('<p> </p>','',$html);

Any suggestions?
Question by:Dennie
  • 3

Expert Comment

ID: 34889418
I think the syntax should be

$html = preg_replace('\<p>\<\p>','' , $html);

to remove <p></p>


Author Comment

ID: 34889438
nope not working

Accepted Solution

Dennie earned 0 total points
ID: 34889493
Ah solved it with my good old regex buddy!
$html = preg_replace('/<[^\/>]*>([\x00\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F\xA0]?)*<\/[^>]*>/','' , $html);

Open in new window

LVL 38

Expert Comment

by:Aaron Tomosky
ID: 34889502
I think you have to escape every less than and greater than  and slash.

Author Closing Comment

ID: 34932104
Solved the problem :)

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
designing in object programming 12 77
Curl & PHP Command Help 4 23
send post values 7 26
Doing something wrong with this PDO Select Statement... 3 8
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Introduction This article is intended for those who are new to PHP error handling (  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question