lunedì 29 gennaio 2007

Eliminare tag Html da una porzione di codice.

Passando alla funzione un porzione di codice html questo verrà restituito privo dei tag specificati nell’array $tags.

//$text => la variabile si aspetta la porzione di codice html da restituire priva dei tag specificati in tags.
//$tags = array() => l’array si aspetta I tag da eliminare dalla porzione di codice html passata alla funzione.

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){
if( preg_match_all( '/<'.$tag.'[^>]*>([^<]*)<\/'.$tag.'>/iu', $text, $found) ){
$text = str_replace($found[0],$found[1],$text);
}
}

return preg_replace( '/(<('.join('|',$tags).')(\\n|\\r|.)*\/>)/iu', '', $text);
}

Esempio di richiamo alla funzione:
strip_selected_tags ($text, $tags = array ("h1"));
?>

Nessun commento: