PHP Regex zerstört meine UTF-8 XMLs

FrazeColder

Lt. Commander
Registriert
Okt. 2013
Beiträge
1.718
Moin zusammen,

Ich habe leider ein Problem. Mein Code downloadet ein paar XML Files aus dem Internet und löscht ein paar Tags aus den jeweiligen XML Files. Das ist auch alles soweit kein Problem und funktioniert auch alles. Es ist auch im UTF-8 Format.

Allerdings seitdem ich noch 3 Methoden hinzugefügt habe mit denen ich die Values bearbeite, sind meine XML Files nicht mehr im UTF-8 Format und ich bekomme diese Fehlermeldung:

Code:
"D:\Anwendung\PHP 7\php-win.exe" C:\Users\Jan\PhpstormProjects\censored\test.php
PHP Warning:  DOMDocument::load(): Input is not proper UTF-8, indicate encoding !
Bytes: 0xE3 0xA4 0x63 0x68 in file:/C:/Users/Jan/PhpstormProjects/censored/data/gamesplanet.xml, line: 1423 in C:\Users\Jan\PhpstormProjects\censored\test.php on line 18
PHP Fatal error:  Uncaught Error: Call to a member function getElementsByTagName() on null in C:\Users\Jan\PhpstormProjects\censored\test.php:23
Stack trace:
#0 C:\Users\Jan\PhpstormProjects\censored\test.php(86): countAd('data/gamesplane...')
#1 {main}
  thrown in C:\Users\Jan\PhpstormProjects\censored\test.php on line 23

Process finished with exit code 255

In Zeile 1423 steht: W㥣hter Von Mittelerde
Und ohne dass ich die Values bearbeite, sprich bevor ich die 3 Mehoden hinzugefügt habe ging alles, die XMLs waren in UTF 8 Format und in Zeile 1423 stand: Wächter von Mittelerde.

Hat jemand eine Idee wie ich das beheben kann und woran das liegt?

Dieser Code verstaucht das:
Code:
function loadTitles($tagName, $path){

    $dom = new DOMDocument('1.0', 'utf-8');
    $dom->preserveWhiteSpace = false;
    $dom->formatOutput = true;
    $dom->load($path);

    $marker = $dom->getElementsByTagName($tagName);

    for ($i = $marker->length - 1; $i >= 0; $i--) {
        $word = $marker->item($i)->textContent;
        $escapedWord = escapWord($word);
        $escapedWord = modifyWord($escapedWord);
        $marker->item($i)->textContent = $escapedWord;
    }

    $dom->saveXML();
    $dom->save($path);
}
function escapWord($string){

    $replaceNothing = [":", ",", ";", "`", "#", "'", "´", "–", "!", "(", ")", ".", "@", "’", "+", "™"];
    $replaceSpace = ["-", "–", "_", "/", ":"];
    $delete = ["Steam", "Eu", "Key", "CD", "Gift", "Edition", "Pack", "Uplay", "Required", "Collection", "Origin", "HD", "Complete", "Digital", "Download", "EA", "Europa", "RPG", "Activated", "Access", "Code", "Limited", "Direct", "Bundle", "Special", "CDKEY", "GLOBAL", "EARLY", "ACCESS", "Card", "Cartel", "Player", "Trade", "DE", "GOG", "Multilanguage", "Multi", "Full", "Only", "UNCUT", "Cut", "Box", "Ps Vita", "VIP", "Rockstar", "Subscription"];

    $string= str_replace($replaceNothing, '', $string);
    $string= str_replace($replaceSpace, ' ', $string);
    $string= preg_replace('~\b(?:' . implode('|', $delete) . ')\b~i', '', $string);
    $string= str_replace("&", ' & ', $string);
    $string= strtolower($string);
    $string= ucwords($string);
    $string= preg_replace('/\bAsia\b/i', 'ASIA', $string);
    $string= preg_replace('/\buk\b/i', 'UK', $string);
    $string= preg_replace('/\bAU\b/i', 'AU', $string);
    $string= preg_replace('/\bXBOX\b/i', 'XBOX ', $string);
    $string= preg_replace('/\bpc\b/i', 'PC', $string);
    $string= preg_replace('/\bus\b/i', 'US', $string);
    $string= preg_replace('/\bru\b/i', 'RUS', $string);
    $string= preg_replace('/\bRUS\b/i', 'RUS', $string);
    $string= preg_replace('/\bPS4\b/i', 'PS4', $string);
    $string= preg_replace('/\bAddon\b/i', 'AddOn', $string);
    $string= preg_replace('/\bPlay Station 4\b/i', 'PS4', $string);
    $string= preg_replace('/\bPs4\b/i', 'PS4', $string);
    $string= preg_replace('/\bPs3\b/i', 'PS3', $string);
    $string= preg_replace('/\bPlayStation 4\b/i', 'PS4', $string);
    $string= preg_replace('/\bPlay Station 3\b/i', 'PS3', $string);
    $string= preg_replace('/\bPlayStation 3\b/i', 'PS3', $string);
    $string= preg_replace('/\bPlayStation Network\b/i', 'PSN', $string);
    $string= preg_replace('/\bPSN\b/i', 'PSN', $string);
    $string= preg_replace('/\bXX\b/i', 'XX', $string);
    $string= preg_replace('/\bXIX\b/i', 'XIX', $string);
    $string= preg_replace('/\bXVIII\b/i', 'XVIII', $string);
    $string= preg_replace('/\bXVII\b/i', 'XVII', $string);
    $string= preg_replace('/\bXVI\b/i', 'XVI', $string);
    $string= preg_replace('/\bXV\b/i', 'XV', $string);
    $string= preg_replace('/\bXIV\b/i', 'XIV', $string);
    $string= preg_replace('/\bXiii\b/i', 'XIII', $string);
    $string= preg_replace('/\bXii\b/i', 'XII', $string);
    $string= preg_replace('/\bXi\b/i', 'XI', $string);
    $string= preg_replace('/\bIX\b/i', 'IX', $string);
    $string= preg_replace('/\bVIII\b/i', 'VIII', $string);
    $string= preg_replace('/\bVII\b/i', 'VII', $string);
    $string= preg_replace('/\bVI\b/i', 'VI', $string);
    $string= preg_replace('/\bV\b/i', 'V', $string);
    $string= preg_replace('/\bIV\b/i', 'IV', $string);
    $string= preg_replace('/\bIII\b/i', 'III', $string);
    $string= preg_replace('/\bII\b/i', 'II', $string);
    $string= preg_replace('/\bdlc\b/i', 'DLC', $string);
    $string= trim(preg_replace('/\s\s+/', ' ', str_replace("\n", " ", $string)));

    return $string;
}
function modifyWord($string){

    if(strpos($string, "Counter Strike Offensive") !== false){
        $newstring = explode("Offensive", $string);;
        $newstring[0] = $newstring[0] . "Global Offensive";
        $string = $newstring[0] . $newstring[1];
    }

    return $string;
}

Vielen Dank und MfG!
 
FrazeColder schrieb:
Hat jemand eine Idee wie ich das beheben kann und woran das liegt?
Da gibts evtl. Vieles. Aber von dem ganzen Code sieht man nichts. Wo wird geladen? Wie wird gespeichert? Wo wird der Inhalt der XML lokal geladen? ...? Die drei Funktionen da oben helfen nichts, bei Zeile 8 ist bereits Schluss.
 
Das liegt daran weil der Code 500 Zeilen hat und ich nicht spammen möchte :)

Die XML Dateien lade ich mir von URLs runter. Beispiel:
Code:
$kinguinXML = 'http://www.kinguin.net/media/feedexport/google_EUR_english_1.xml';

Jedes mal wenn ich Daten öffnen oder schließen will sieht das so aus:
Code:
$dom = new DOMDocument('1.0', 'utf-8');
    $dom->preserveWhiteSpace = false;
    $dom->formatOutput = true;
    $dom->load($kinguinPath);

//CODE
//CODE
//CODE

    $dom->saveXML();
    $dom->save($kinguinPath);

Gespeichert wird lokal:
Code:
    $dom->saveXML();
    $dom->save($kinguinPath);
   $kinguinPath = 'data/kinguin.xml';
 

Ähnliche Themen

Zurück
Oben