<?php

// Mod DZ 2103-12-18
$table = 'items';
$limit = 10000;

//ini_set("memory_limit","1024M");
//set_time_limit(60000);
// get data
// connect import database
function replace_num_entity($ord) {
    $ord = $ord[1];
    if (preg_match('/^x([0-9a-f]+)$/i', $ord, $match)) {
        $ord = hexdec($match[1]);
    } else {
        $ord = intval($ord);
    }

    $no_bytes = 0;
    $byte = array();

    if ($ord < 128) {
        return chr($ord);
    } elseif ($ord < 2048) {
        $no_bytes = 2;
    } elseif ($ord < 65536) {
        $no_bytes = 3;
    } elseif ($ord < 1114112) {
        $no_bytes = 4;
    } else {
        return;
    }

    switch ($no_bytes) {
        case 2: {
                $prefix = array(31, 192);
                break;
            }
        case 3: {
                $prefix = array(15, 224);
                break;
            }
        case 4: {
                $prefix = array(7, 240);
            }
    }

    for ($i = 0; $i < $no_bytes; $i++) {
        $byte[$no_bytes - $i - 1] = (($ord & (63 * pow(2, 6 * $i))) / pow(2, 6 * $i)) & 63 | 128;
    }

    $byte[0] = ($byte[0] & $prefix[0]) | $prefix[1];

    $ret = '';
    for ($i = 0; $i < $no_bytes; $i++) {
        $ret .= chr($byte[$i]);
    }

    return utf8_decode($ret);
}

/* $connection=mysql_connect('localhost', 'root', '1234') or die ("Verbindungsversuch fehlgeschlagen");
  mysql_select_db('intelectra_shop__', $connection) or die("Konnte die Datenbank nicht waehlen.");
 */
$connection = mysql_connect('localhost', 'int-db-sql-shop', 'Msc7t24?381L') or die("Verbindungsversuch fehlgeschlagen");
mysql_select_db('webshop-sql', $connection) or die("Konnte die Datenbank nicht waehlen.1");
echo "get items";
$query = mysql_query("SELECT * FROM " . $table . " ORDER BY id LIMIT " . $limit) or die("Anfrage nicht erfolgreich");
$data = array();
echo "set description";
echo '<hr>' . "\r\n";

while ($obj = mysql_fetch_object($query)) {

    $desc = strip_tags(str_replace('<br>', ' ', $obj->detail_description));
    $desc = str_replace(array(
        "\r\n", "\r", "\n", "\t"
            ), '', $desc);

    // Mod DZ 2103-12-18
    echo $obj->id, '<br>' . "\r\n";
    echo mb_detect_encoding($desc, 'UTF-8, ISO-8859-1'), '<br>' . "\r\n";
    echo $desc, '<br>' . "\r\n";
    
    $hasHexStuff = preg_match('/&#([0-9a-fx]+);/mi', $desc);
    
    $desc = preg_replace_callback('/&#([0-9a-fx]+);/mi', 'replace_num_entity', $desc);
        
    echo $desc, '<br>' . "\r\n";
        
    if (mb_detect_encoding($desc, 'UTF-8, ISO-8859-1') === 'UTF-8') {
        echo 'isutf8<br>' . "\r\n";
        $desc = utf8_decode($desc);
    } else if ($hasHexStuff) {
        //$desc = utf8_decode($desc);
    }
    
    $desc = htmlentities($desc);
//    $desc = preg_replace("@[\s]{2,}@si", ' ', $desc);
//    $desc = trim($desc);
    
    echo $desc . "\r\n";
    echo '<hr>' . "\r\n";
    // End mod

    $sql = "
		UPDATE " . $table . "
		SET
                    short_description='" . $desc . "'
		WHERE id=" . $obj->id . "
	";
    $query2 = mysql_query($sql);

    // echo mysql_error(), '<hr>';
//      echo $sql, '<hr><hr>';

    if (!$query2) {
//                echo $sql;
//                echo "<br />";
    }
}
echo "ende";
?>