A universal strip_selected_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
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

A universal strip_selected_tags?

Post by Citizen »

I'm currently using this function:

Code: Select all

function strip_selected_tags($text, $tags = array())
{
    $args = func_get_args();
    $text = array_shift($args);
    $tags = func_num_args() > 2 ? array_diff($args,array($text))  : (array)$tags;
    foreach ($tags as $tag){
        while(preg_match('/<'.$tag.'(|\W[^>]*)>(.*)<\/'. $tag .'>/iusU', $text, $found)){
            $text = str_replace($found[0],$found[2],$text);
        }
    }
    return preg_replace('/(<('.join('|',$tags).')(|\W.*)\/>)/iusU', '', $text);
}
But its not working on some versions of php. Does anyone know of a good, reliable, strip_selected_tags function?
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: A universal strip_selected_tags?

Post by alex.barylski »

A better approach is probably to use something along the lines of HTML_Purifier:

http://htmlpurifier.org/
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Re: A universal strip_selected_tags?

Post by Citizen »

Literally all I need to do is remove a few html tags. This is a small modification to existing software so a working version of strip_selected_tags would work perfect. Htmlpurifier is a big heavy for this simple of a task.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: A universal strip_selected_tags?

Post by alex.barylski »

Without digging into you code, how exactly is your implementation different from strip_tags()???
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Re: A universal strip_selected_tags?

Post by Citizen »

Usage:

Code: Select all

function strip_selected_tags($text, $tags = array())
{
    $args = func_get_args();
    $text = array_shift($args);
    $tags = func_num_args() > 2 ? array_diff($args,array($text))  : (array)$tags;
    foreach ($tags as $tag){
        while(preg_match('/<'.$tag.'(|\W[^>]*)>(.*)<\/'. $tag .'>/iusU', $text, $found)){
            $text = str_replace($found[0],$found[2],$text);
        }
    }
    return preg_replace('/(<('.join('|',$tags).')(|\W.*)\/>)/iusU', '', $text);
}
 
if($options['removetables'] == 1){
    $sHtml = strip_selected_tags($sHtml, array('table','tr','td','tbody'));
}
if($options['removeimages'] == 1){
    $sHtml = strip_selected_tags($sHtml, array('img'));
}
Does not work with php 5.2.6
pkbruker
Forum Commoner
Posts: 32
Joined: Sun Aug 03, 2008 9:36 am
Location: Oslo, Norway

Re: A universal strip_selected_tags?

Post by pkbruker »

Hockey wrote:Without digging into you code, how exactly is your implementation different from strip_tags()???
Because strip_tags only allows you to specify tags which should not be stripped.

But, with the correct regex, your function shouldn't be too hard to make. Google a bit on regex for removing HTML tags, then modify to work for specific tags.
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Re: A universal strip_selected_tags?

Post by Citizen »

The best I can get with regex is to remove the tags, but not without removing the content within the tags.
Post Reply