Removing unwanted tags

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Removing unwanted tags

Post by Addos »

Hi,
I’m trying to do the following.

if (<ul></ul> appear side by side within a script) remove them {
}else { leave all other <ul></ul> in place that contain content within them.


I have a script that’s producing some extra <ul></ul> i.e. without text within them and simply want to remove them before I print to the browser.

I’ve tinkered with the following and tried strip_tags too but neither are really giving me the result. Is there a function that I can use to do this?
Thanks for any help.

Code: Select all

<?PHP
$needs_cleaning = "<li><ul></ul></li>" ;
$cleaned = str_replace($needs_cleaning,  "<ul></ul>","") ;
 
print $cleaned ;
?>
Reviresco
Forum Contributor
Posts: 172
Joined: Tue Feb 19, 2008 4:18 pm
Location: Milwaukee

Re: Removing unwanted tags

Post by Reviresco »

Code: Select all

 
<?PHP
$needs_cleaning = "<li><ul></ul></li>" ;
$cleaned = str_replace("<ul></ul>",  "", $needs_cleaning ) ;
print $cleaned ;
?>
 

Code: Select all

 
$needs_cleaning = "<li><ul></ul></li>" ;
$cleaned = strip_tags($needs_cleaning, '<li>');
 
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Re: Removing unwanted tags

Post by Addos »

Okay let me explain more. The code below has 2 unwanted <ul></ul> tags and I want to pass this through the strip_tag function (on something similar) that will remove these if found together however I need the <ul></ul> tags elsewhere in the script so I just want to remove only those that appear side by side. Is this possible?
Thanks for the reply
Last edited by Addos on Tue Aug 12, 2008 2:09 pm, edited 1 time in total.
Reviresco
Forum Contributor
Posts: 172
Joined: Tue Feb 19, 2008 4:18 pm
Location: Milwaukee

Re: Removing unwanted tags

Post by Reviresco »

You'll need to use the str_replace because the strip_tags will take out all of the <ul> tags.
Post Reply