Ver Mensaje Individual
  #6 (permalink)  
Antiguo 17/03/2008, 15:19
okram
Invitado
 
Mensajes: n/a
Puntos:
Re: Eliminar etiquetas html

Siempre es bueno revisar los comentarios dejados en el manual de PHP. Si revisas el manual de la función strip_tags() (que hace exactamente lo que quieres pero al revés), y te vas a leer los comentarios dejados, verás que hay más de una solución posteada a tu problema.

Código PHP:
# http://www.php.net/manual/es/function.strip-tags.php#76045
function strip_selected_tags($text$tags = array())
{
    
$args func_get_args();
    
$text array_shift($args);
    
$tags func_num_args() > 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);
}
 
# USO
 
// Borrar los tags <b></b> y <a></a>
 
$str "Hola <b>Mundo</b>, este es <a href=\"http://www.google.com\">mi</a> primer <i>script</i>";
 
echo 
strip_selected_tags($str"b""a"); 
Saludos,