1
0
Prechádzať zdrojové kódy

Fix encoding

Thanks to https://github.com/technosophos/querypath/issues/94#issuecomment-45725727.
Sunny 2 rokov pred
rodič
commit
14c347ced6
3 zmenil súbory, kde vykonal 18 pridanie a 2 odobranie
  1. 2 1
      pages/artist.php
  2. 2 1
      pages/release.php
  3. 14 0
      utilities/dom.php

+ 2 - 1
pages/artist.php

@@ -1,7 +1,8 @@
 <?php
+  include "../utilities/dom.php";
   require "../modules/querypath/src/qp.php";
 
-  $document = htmlqp(file_get_contents("https://" . urlencode($_GET["name"]) . ".bandcamp.com/music"));
+  $document = htmlqp(encode_document(file_get_contents("https://" . urlencode($_GET["name"]) . ".bandcamp.com/music")));
 
   $title = $document->find("#band-name-location .title")->text();
 ?>

+ 2 - 1
pages/release.php

@@ -1,7 +1,8 @@
 <?php
+  include "../utilities/dom.php";
   require "../modules/querypath/src/qp.php";
 
-  $document = htmlqp(file_get_contents("https://" . urlencode($_GET["artist"]) . ".bandcamp.com/" . urlencode($_GET["type"]) . "/" . urlencode($_GET["name"])));
+  $document = htmlqp(encode_document(file_get_contents("https://" . urlencode($_GET["artist"]) . ".bandcamp.com/" . urlencode($_GET["type"]) . "/" . urlencode($_GET["name"]))));
   $json = json_decode($document->find("script[data-tralbum]")->attr("data-tralbum"));
 
   $title = $json->current->title;

+ 14 - 0
utilities/dom.php

@@ -0,0 +1,14 @@
+<?php
+  function encode_document($text) {
+    $document = new DOMDocument();
+    $document->loadHTML("<?xml encoding=\"UTF-8\">" . $text);
+
+    foreach ($document->childNodes as $node) {
+      if ($node->nodeType === XML_PI_NODE)
+        $document->removeChild($node);
+    };
+
+    $document->encoding = "UTF-8";
+    return $document;
+  };
+?>