3v4l.org

run code in 300+ PHP versions simultaneously
<?php $html = <<<HTML <!DOCTYPE html> <html> <head> <title></title> </head> <body> <table class="table-list table table-responsive table-striped" border="1"> <thead> <tr> <th class="coll-1 name">name</th> <th class="coll-2">height</th> <th class="coll-3">weight</th> <th class="coll-date">date</th> <th class="coll-4"><span class="info">info</span></th> <th class="coll-5">country</th> </tr> </thead> <tbody> <tr> <td class="coll-1 name"> <a href="/username/Jhon Doe/" class="icon"><i class="flaticon-user"></i></a> <a href="/username/Jhon Doe/">Jhon Doe</a> </td> <td class="coll-2 height">45</td> <td class="coll-3 weight">50</td> <td class="coll-date">9am May. 16th</td> <td class="coll-4 size mob-info">abcd</td> <td class="coll-5 country"><a href="/country/CA/">CA</a></td> </tr> <tr> <td class="coll-1 name"> <a href="/username/Kasim Shk/" class="icon"><i class="flaticon-user"></i></a> <a href="/username/Kasim Shk/">Kasim Shk</a> </td> <td class="coll-2 height">33</td> <td class="coll-3 weight">54</td> <td class="coll-date">Mar. 14th '18</td> <td class="coll-4 size mob-info">ijkl</td> <td class="coll-5 country"><a href="/country/UAE/">UAE</a></td> </tr> </tbody> </table> </body> </html> HTML; $dom = new DOMDocument; $dom->loadHTML($html); $xpath = new DOMXPath($dom); foreach ($xpath->query('//tbody/tr') as $tr) { $tmp = []; // reset the temporary array so previous entries are removed foreach ($xpath->query("td[@class]", $tr) as $td) { $key = preg_match('~[a-z]+$~', $td->getAttribute('class'), $out) ? $out[0] : 'no_class'; if ($key === "name") { $tmp['user_link'] = $xpath->query("a[@class = 'icon']", $td)[0]->getAttribute('href'); } $tmp[$key] = trim($td->textContent); } $tmp['date'] = date("M. dS 'y", strtotime(preg_replace('~\.|\d+[ap]m *~', '', $tmp['date']))); $result[] = $tmp; } var_export($result); echo "\n----\n"; echo json_encode($result);
Output for 5.6.3 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.31, 8.2.0 - 8.2.27, 8.3.0 - 8.3.15, 8.4.1 - 8.4.2
array ( 0 => array ( 'user_link' => '/username/Jhon Doe/', 'name' => 'Jhon Doe', 'height' => '45', 'weight' => '50', 'date' => 'May. 16th \'23', 'info' => 'abcd', 'country' => 'CA', ), 1 => array ( 'user_link' => '/username/Kasim Shk/', 'name' => 'Kasim Shk', 'height' => '33', 'weight' => '54', 'date' => 'Jan. 01st \'70', 'info' => 'ijkl', 'country' => 'UAE', ), ) ---- [{"user_link":"\/username\/Jhon Doe\/","name":"Jhon Doe","height":"45","weight":"50","date":"May. 16th '23","info":"abcd","country":"CA"},{"user_link":"\/username\/Kasim Shk\/","name":"Kasim Shk","height":"33","weight":"54","date":"Jan. 01st '70","info":"ijkl","country":"UAE"}]
Output for 5.6.0 - 5.6.2
Fatal error: Cannot use object of type DOMNodeList as array in /in/nHKef on line 59
Process exited with code 255.
Output for 5.2.17
Parse error: syntax error, unexpected '[' in /in/nHKef on line 55
Process exited with code 255.

preferences:
52.65 ms | 408 KiB | 5 Q