Table Sorting
loadHTMLFile($url); $xpath = new DOMXPath($doc); $table = $xpath->query('//table[@id="Top_table"]')->item(0); if ($table) { $parsedUrl = parse_url($url); $baseUrl = $parsedUrl['scheme'] . '://' . $parsedUrl['host']; $tableRows = []; $rows = $table->getElementsByTagName('tr'); foreach ($rows as $row) { $cells = $row->getElementsByTagName('td'); if ($cells->length >= 3) { $nameCell = $cells->item(1); $nameLink = $nameCell->getElementsByTagName('a')->item(0); $name = ''; $nameUrl = ''; if ($nameLink) { $name = trim($nameLink->textContent); $nameRelativeUrl = $nameLink->getAttribute('href'); $nameUrl = $baseUrl . $nameRelativeUrl; } $categoryCell = $cells->item(3); $category = ''; if ($categoryCell) { $category = trim($categoryCell->textContent); } $tableRows[] = [ 'name' => $name, 'nameUrl' => $nameUrl, 'category' => $category ]; } } echo '
'; foreach ($tableRows as $row) { echo '
'; echo '

' . $row['name'] . '

' . $row['category']; // Initialize cURL $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $row['nameUrl']); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'); $response = curl_exec($curl); if ($response === false) { die('Error: ' . curl_error($curl)); } $startPos = strpos($response, ''); $endPos = strpos($response, ''); if ($startPos !== false) { $content = substr($response, $startPos, $endPos - $startPos); $content = preg_replace_callback('/(]+src=")([^"]+)("[^>]*>)/i', function ($matches) { $imageUrl = $matches[2]; $imageData = base64_encode(file_get_contents($imageUrl)); return $matches[1] . 'data:image/png;base64,' . $imageData . $matches[3]; }, $content); echo '
' . $content . '
'; } else { $fallbackStartPos = strpos($response, '
'); $fallbackEndPos = strpos($response, '
', $fallbackStartPos); if ($fallbackStartPos !== false && $fallbackEndPos !== false) { $fallbackContent = substr($response, $fallbackStartPos, $fallbackEndPos - $fallbackStartPos + 8); $fallbackContent = preg_replace_callback('/(]+src=")([^"]+)("[^>]*>)/i', function ($matches) { $imageUrl = $matches[2]; $imageData = base64_encode(file_get_contents($imageUrl)); return $matches[1] . 'data:image/png;base64,' . $imageData . $matches[3]; }, $fallbackContent); echo '
' . $fallbackContent . '
'; } } curl_close($curl); echo '
'; } echo '
'; } else { echo 'No table found.'; } } ?>