Solved

php strip empty html tags

Posted on 2011-02-14
5
401 Views
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?
0
Comment
Question by:Dennie
  • 3
5 Comments
 
LVL 9

Expert Comment

by:sshah254
ID: 34889418
I think the syntax should be

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

to remove <p></p>

Ss
0
 

Author Comment

by:Dennie
ID: 34889438
nope not working
0
 

Accepted Solution

by:
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

0
 
LVL 39

Expert Comment

by:Aaron Tomosky
ID: 34889502
I think you have to escape every less than and greater than  and slash.
\<p\>\<\/p\>
0
 

Author Closing Comment

by:Dennie
ID: 34932104
Solved the problem :)
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

808 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