Re: Eliminar <STYLE> y <javascript> del HTML

From: Date: Fri, 31 Oct 2003 21:37:53 +0000
Subject: Re: Eliminar <STYLE> y <javascript> del HTML
References: 1  Groups: php.general.es 
Request: Send a blank email to general-es+get-42145@lists.php.net to get a copy of this message
Hola, Yo uso esta funcion, puedes especificarle el tag que quieras y te borrara el tag y su contenido. Saludos Christian -------------------------------------------------------------- <?PHP function strip_tag_script($html,$tag) { $pos1 = false; $pos2 = false; do { if ($pos1 !== false && $pos2 !== false) { $first = NULL; $second = NULL; if ($pos1 > 0) $first = substr($html, 0, $pos1); if ($pos2 < strlen($html) - 1) $second = substr($html, $pos2); $html = $first . $second; } preg_match("/<".$tag."[^>]*>/i", $html, $matches); $str1 =& $matches[0]; preg_match("/<\/".$tag.">/i", $html, $matches); $str2 =& $matches[0]; $pos1 = strpos($html, $str1); $pos2 = strpos($html, $str2); if ($pos2 !== false) $pos2 += strlen($str2); } while ($pos1 !== false && $pos2 !== false); return $html; } $paginaweb='<html> <script language="JavaScript" type="text/JavaScript"> <!-- function MM_reloadPage(init) { //reloads the window if Nav4 resized if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) { document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }} else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload(); } MM_reloadPage(true); //--> </script> <STYLE> A.verdb:hover { COLOR: #eeaf00 } .tahoa { FONT-SIZE: 8pt; FONT-FAMILY: Tahoma } </STYLE> <STYLE> A.verdb:hover { COLOR: #eeaf00 } .tahoa { FONT-SIZE: 8pt; FONT-FAMILY: Tahoma } </STYLE> <p>texto de la pagina web</p> '; $pagina=strip_tag_script($paginaweb,'script'); $paginaweb=strip_tag_script($pagina,'style'); echo $paginaweb; ?> -------------------------------------------------------------- ----- Original Message ----- From: "Gabriel" <gabriel@elcaramelito.com> To: <lista@phpes.com> Sent: Friday, October 31, 2003 1:32 PM Subject: [PHP-ES] Eliminar <STYLE> y <javascript> del HTML Es un tema que se ha tratado varias veces en la lista pero ahora necesito ser más específico. Generalmente se pregunta cómo sacar las etiquetas html. Para eso se ha propuesto la bastante efectiva función strip_tags. El problema es que tengo etiquetas <STYLE> que ocupan más de una línea por ejemplo: <STYLE> A.verdb:hover { COLOR: #eeaf00 } .tahoa { FONT-SIZE: 8pt; FONT-FAMILY: Tahoma } </STYLE> y dicha función de php me elimina la etiqueta pero no su contenido. O sea en el texto limpio me queda: A.verdb:hover { COLOR: #eeaf00 } .tahoa { FONT-SIZE: 8pt; FONT-FAMILY: Tahoma } Lo mismo pasa con códigos javascript. Alguien le ha encontrado una solución? Saludos Gabriel

« previous php.general.es (#42145) next »