Solved

php strip empty html tags

Posted on 2011-02-14
5
405 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses four methods for overlaying images in a container on a web page
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…
The viewer will learn how to dynamically set the form action using jQuery.

738 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