title: Parsing remote website contents into json object
body: <p>I want to read specific details from a website into a json object. This is the Website's html:</p>
  
  <pre><code>&lt;body&gt;
  &lt;a href="https://myanimelist.net/anime/1/Cowboy_Bebop" class="hovertitle"&gt;Cowboy Bebop (1998)&lt;/a&gt;
  &lt;div style="margin-top: 8px; margin-bottom: 10px;"&gt;In the year 2071, humanity has colonized several of the planets and moons of the solar system leaving the now uninhabitable surface of planet Earth behind. The Inter Solar System Police attempts to ke... &lt;a href="https://myanimelist.net/anime/1/Cowboy_Bebop"&gt;read more&lt;/a&gt;&lt;/div&gt;
  &lt;span class="dark_text"&gt;Genres:&lt;/span&gt; Action, Adventure, Comedy, Drama, Sci-Fi, Space&lt;br&gt;
  &lt;span class="dark_text"&gt;Status:&lt;/span&gt; Finished Airing&lt;br&gt;
  &lt;span class="dark_text"&gt;Type:&lt;/span&gt; TV&lt;br&gt;
  &lt;span class="dark_text"&gt;Episodes:&lt;/span&gt; 26&lt;br&gt;
  &lt;span class="dark_text"&gt;Score:&lt;/span&gt; 8.81 &lt;small&gt;(scored by 345,892 users)&lt;/small&gt;&lt;br&gt;
  &lt;span class="dark_text"&gt;Ranked:&lt;/span&gt; #26&lt;br&gt;
  &lt;span class="dark_text"&gt;Popularity:&lt;/span&gt; #35&lt;br&gt;
  &lt;span class="dark_text"&gt;Members:&lt;/span&gt; 664,034&lt;br&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>I'm only looking for the contents of the spans with <code>class="dark_text"</code> and the first 5 entries of them. The finished json object should look like this:</p>
  
  <pre><code>{
     "genres": [ "Action", "Adventure", "Comedy", "Drama", "Sci-Fi", "Space" ], // Array
     "status": "Finished Airing",
     "type": "TV",
     "episodes": "26",
     "score": "8.81"
  }
  </code></pre>
  
  <p>My approach to doing this was:</p>
  
  <pre><code>function ParseDataIntoJson($html)
  {
      $dom = new DOMDocument;
      @$dom-&gt;loadHTML($html); // supress errors
  
      $xpath = new DOMXPath($dom);
      $items = $xpath-&gt;query("//span[@class='dark_text']"); // spans itself
      $values = $xpath-&gt;query("//span[@class='dark_text']/following-sibling::text()"); // text after the span
  
      $item_array = array();
      $value_array = array();
  
      for ($i = 0; $i &lt; 5; $i++) // only first 5 entries
      {
          $item = strtolower(rtrim($items[$i]-&gt;textContent, ":")); // remove : at the end and convert it to lowercase string
  
          $item_array[$i] = $item;
  
          $value = rtrim(ltrim($values[$i]-&gt;textContent, " "), " "); // remove leading/ending space
  
          if($i == 0 &amp;&amp; strpos($value, ', ')) // if i = 0 (genres entry) and it contains ", "
              $value = explode(", ", $value); // split into array using ", " as delimiter
  
          $value_array[$i] = $value; // if $value is an array after splitting, will this still work?
      }
  
      // generate json from data and return it
  
      // return $json;
  }
  </code></pre>
  
  <p>As you can see it's pretty hardcoded (that's because i'm just getting into php) and the converting to json part is still missing. So if anyone of you could help me out that would be greatly appreciated. Thanks in advance!</p>