Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, use the API to scrape the wikitext as JSON. Do that by fetching this URL:</p> <pre class="lang-none prettyprint-override"><code>https://en.wikipedia.org/w/api.php?action=parse&amp;page=Nick_Diaz&amp;prop=wikitext&amp;format=json </code></pre> <p>Then, assuming the result is in <code>$data</code>, do this:</p> <pre><code>$data = json_decode($data, true); $data = $data['parse']['wikitext']['*']; </code></pre> <p>Now all the wikitext is in <code>$data</code>. It just so happens that the total is calculated from five fields:</p> <ul> <li><code>mma_kowin</code> &mdash; Knockout</li> <li><code>mma_subwin</code> &mdash; Submission</li> <li><code>mma_decwin</code> &mdash; Decision</li> <li><code>mma_dqwin</code> &mdash; Disqualification</li> <li><code>mma_otherwin</code> &mdash; Other</li> </ul> <p>You can parse these fields out pretty easily with a regular expression:</p> <pre><code>/^\s*\|\s*mma_([a-z]+)win\s*=\s*(\d*)/m </code></pre> <p>You'll want to find all matches of that regular expression in <code>$data</code>. The first group will be <code>ko</code>, <code>sub</code>, etc. The second match will be a string representation of the number or an empty string for zero. Then you'll want to add all of those numbers up, and bam, you've got it.</p> <p>All together now:</p> <pre><code>&lt;?php $data = file_get_contents("https://en.wikipedia.org/w/api.php?action=parse&amp;page=Nick_Diaz&amp;prop=wikitext&amp;format=json"); $data = json_decode($data, true); $data = $data['parse']['wikitext']['*']; preg_match_all('/^\s*\|\s*mma_([a-z]+)win\s*=\s*(\d*)/m', $data, $matches, PREG_SET_ORDER); $wins = array(); foreach($matches as $match) { $wins[$match[1]] = (int)$match[2]; } ?&gt; &lt;h1&gt;Nick Diaz's Wins&lt;/h1&gt; &lt;table border="1"&gt; &lt;tr&gt;&lt;th&gt;Means&lt;/th&gt;&lt;th&gt;Wins&lt;/th&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;Knockout&lt;/td&gt;&lt;td&gt;&lt;?php echo $wins['ko']; ?&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;Submission&lt;/td&gt;&lt;td&gt;&lt;?php echo $wins['sub']; ?&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;Decision&lt;/td&gt;&lt;td&gt;&lt;?php echo $wins['dec']; ?&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;Disqualification&lt;/td&gt;&lt;td&gt;&lt;?php echo $wins['dq']; ?&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;Other&lt;/td&gt;&lt;td&gt;&lt;?php echo $wins['other']; ?&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;&lt;strong&gt;Total&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;strong&gt;&lt;?php echo array_sum($wins); ?&gt;&lt;/strong&gt;&lt;/td&gt;&lt;/tr&gt; &lt;/table&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload