C O N O S C E N Z A
web data management
Il nostro è un team che si occupa ormai da piu' di un decennio della risoluzione delle problematiche informatiche, in particolare ci piace poter offrire la nostra competenza in ambito telematico, dalla progettazione su misura della vostra infrastruttura web, alla realizzazione del vostro software e dei vostri portali.
Web

php script to close unclosed html tags - chiude i tag html aperti

15/03/2012
A PHP script to close unclosed html tags on each table fields, you must specify the table and db credentials.

Questo script chiude i tag html lasciati aperti su ogni campo della tabella del db che specificheremo nelle prime righe di codice.


<?

$link = mysql_connect('localhost', 'user', 'password');
if (!$link) {
die('No Connection - Non posso Connettermi: ' . mysql_error());
}

mysql_select_db('yourdb');

//mysql_query("set names utf8;");

//do you need to insert above more tags to close ? hai bisogno di chiudere altri tag oltre a quelli qui sotto?
$htmlTags = array('meta', 'img', 'link', 'br', 'hr', 'input');

//set the table to parse - inserisci il nome della tabella da modificare
$tbl_name='your_table';


$showFieldQuery="show COLUMNS FROM $tbl_name";

$retval = mysql_query( $showFieldQuery , $link );

if(! $retval )
{
die('MYSQL Error: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
$fields[] = $row['Field'];

}

echo "<h1>Parsing $tbl_name <br />In Esecuzione su $tbl_name</h1>";

foreach($htmlTags as $key => $tag){
echo "<h2>Processing $tag <br /> processo $tag</h2>";
foreach($fields as $MYfield){
$query = "SELECT * FROM `$tbl_name` WHERE `$MYfield` like \"%<$tag%\"";
$retval = mysql_query( $query , $link );
if(! $retval )
{
die('Errore MYSQL: ' . mysql_error());
}
while($rowSTRINGS = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "<b>Trovato match a identificativo (campo[0]) ".$rowSTRINGS[$fields[0]]." su campo ".$MYfield.", procedo ad analizzare</b><br />";
echo "<b>Found match at id (field[0]) ".$rowSTRINGS[$fields[0]]." on field ".$MYfield.", go on in analysis</b><br />";
$tmpFieldString = $rowSTRINGS[$MYfield];
$controlabel=0;

while(strpos($tmpFieldString,'<'.$tag) !== false){
$posTag = strpos($tmpFieldString,'<'.$tag);
$posTag++;
$TMPtext = substr($tmpFieldString , $posTag);

$posTagEnd = strpos($TMPtext,'>');
$posTagEnd++;
$TMPtextString = substr($TMPtext , 0, $posTagEnd);
$ClosureText = substr($TMPtext ,$posTagEnd-2, 1);
if($ClosureText != '/'){
echo "<h4>Need Replace <font color=brown>".$TMPtextString."</font></h4>";

$texttoreplace = str_replace(">"," />",$TMPtextString);
echo "<h6>$TMPtextString <br /> replaced by / diventa <br /> $texttoreplace </h6>";

$rowSTRINGS[$MYfield] = str_replace("<$TMPtextString","<$texttoreplace",$rowSTRINGS[$MYfield]);
$controlabel=1;
}else{
echo 'OK <small> @ <small>'. $TMPtextString.'</small></small>';
}
echo '<br>';

$tmpFieldString = substr($tmpFieldString , $posTag+1);
}

$sqlUpper= "UPDATE `$tbl_name` SET `$MYfield` = '".str_replace("'",''',$rowSTRINGS[$MYfield])."' where `".$fields[0]."` = ".$rowSTRINGS[$fields[0]];
if($controlabel==1){
if( !mysql_query($sqlUpper) ) {echo "*update failed*/".mysql_error().$sqlUpper; die();}
}

}



}


}

mysql_close($link);


?>