include 'config.php';
//this script replaces characters which are valid utf8
//but invalid in XML (there are just a few)
$ascii_id = 'cumbojd';
$c = Dase_DBO_Collection::get($db, $ascii_id);
foreach ($c->getItems() as $item) {
    $item = clone $item;
    print "working\n";
    $item->deleteDocs();
    $item->storeDoc();
    foreach ($item->getValues() as $value) {
        $str = $value->value_text;
        //		print $str."\n";
        if ($str != strip_invalid_xml_chars2($str)) {
            $value->value_text = strip_invalid_xml_chars2($str);
            $value->update();
            print "updated item {$item->serial_number}\n";
        }
    }
}
function strip_invalid_xml_chars2($in)
{
    $out = "";
    $length = strlen($in);
    for ($i = 0; $i < $length; $i++) {
        $current = ord($in[$i]);
        if ($current == 0x9 || $current == 0xa || $current == 0xd || $current >= 0x20 && $current <= 0xd7ff || $current >= 0xe000 && $current <= 0xfffd || $current >= 0x10000 && $current <= 0x10ffff) {
            $out .= chr($current);
        } else {
            $out .= " ";
示例#2
0
<?php

include 'config.php';
$start = microtime(1);
$str = "\n\tLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum\n\tLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum\n\tLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum\n\tLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum\n\tLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum\n\tLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum\n\t";
$iters = 1000;
foreach (range(1, $iters) as $num) {
    $out = strip_invalid_xml_chars2($str);
}
$stop = microtime(1);
print ($stop - $start) / $iters . " seconds each\n";
function strip_invalid_xml_chars2($in)
{
    $out = "";
    $length = strlen($in);
    for ($i = 0; $i < $length; $i++) {
        $current = ord($in[$i]);
        if ($current == 0x9 || $current == 0xa || $current == 0xd || $current >= 0x20 && $current <= 0xd7ff || $current >= 0xe000 && $current <= 0xfffd || $current >= 0x10000 && $current <= 0x10ffff) {
            $out .= chr($current);
        } else {
            print $current . "\n";
            $out .= " ";
        }
    }
    return $out;
}
<?php

include 'config.php';
//this script replaces characters which are valid utf8
//but invalid in XML (there are just a few)
$tis = new Dase_DBO_TagItem($db);
foreach ($tis->find() as $ti) {
    if ($ti->annotation != strip_invalid_xml_chars2($ti->annotation)) {
        $tag = $ti->getTag();
        print $tag->ascii_id . "\n";
        //$ti->annotation = strip_invalid_xml_chars2($ti->annotation);
        //$ti->update();
        file_put_contents('bad', $ti->annotation);
        print "found bad xml in tag_item {$ti->id}\n";
        print $tag->dase_user_id . "\n";
        //print $ti->annotation."\n";
    }
}
function strip_invalid_xml_chars2($in)
{
    $out = "";
    $length = strlen($in);
    for ($i = 0; $i < $length; $i++) {
        $current = ord($in[$i]);
        if ($current == 0x9 || $current == 0xa || $current == 0xd || $current >= 0x20 && $current <= 0xd7ff || $current >= 0xe000 && $current <= 0xfffd || $current >= 0x10000 && $current <= 0x10ffff) {
            $out .= chr($current);
        } else {
            print $current . "\n";
            $out .= " ";
        }
    }