mirror of
https://github.com/claradex/nativegallery.git
synced 2024-11-15 03:31:10 +03:00
44 lines
863 B
PHP
44 lines
863 B
PHP
|
<?php
|
||
|
|
||
|
namespace App\Services;
|
||
|
use DOMDocument, DOMXPath;
|
||
|
|
||
|
class HTMLParser
|
||
|
{
|
||
|
|
||
|
|
||
|
public static function parse($url, $mask)
|
||
|
{
|
||
|
|
||
|
$ch = curl_init();
|
||
|
|
||
|
curl_setopt($ch, CURLOPT_URL, $url);
|
||
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||
|
|
||
|
$response = curl_exec($ch);
|
||
|
|
||
|
if (curl_errno($ch)) {
|
||
|
echo 'Ошибка cURL: ' . curl_error($ch);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
curl_close($ch);
|
||
|
|
||
|
$dom = new DOMDocument;
|
||
|
|
||
|
@$dom->loadHTML($response);
|
||
|
|
||
|
$xpath = new DOMXPath($dom);
|
||
|
|
||
|
$nodes = $xpath->query($mask);
|
||
|
|
||
|
if ($nodes->length > 0) {
|
||
|
$firstNode = $nodes->item(0)->textContent;
|
||
|
$cleanedContent = ltrim($firstNode);
|
||
|
return $cleanedContent;
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|
||
|
}
|