Page 1 of 1
A universal strip_selected_tags?
Posted: Wed Aug 06, 2008 2:47 pm
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?
Re: A universal strip_selected_tags?
Posted: Wed Aug 06, 2008 3:31 pm
by alex.barylski
A better approach is probably to use something along the lines of HTML_Purifier:
http://htmlpurifier.org/
Re: A universal strip_selected_tags?
Posted: Wed Aug 06, 2008 4:03 pm
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.
Re: A universal strip_selected_tags?
Posted: Wed Aug 06, 2008 4:12 pm
by alex.barylski
Without digging into you code, how exactly is your implementation different from strip_tags()???
Re: A universal strip_selected_tags?
Posted: Thu Aug 07, 2008 2:23 pm
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
Re: A universal strip_selected_tags?
Posted: Fri Aug 08, 2008 6:36 am
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.
Re: A universal strip_selected_tags?
Posted: Tue Aug 12, 2008 1:34 pm
by Citizen
The best I can get with regex is to remove the tags, but not without removing the content within the tags.