2024-01-22 17:08:45 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
class PageScrapper
|
|
|
|
{
|
|
|
|
private string $url;
|
|
|
|
private string $contentMarker;
|
|
|
|
|
|
|
|
public function __construct($url, $contentMarker = '<div class=["\']content_info["\']>')
|
|
|
|
{
|
|
|
|
$this->url = $url;
|
|
|
|
$this->contentMarker = $contentMarker;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getHTML()
|
|
|
|
{
|
2024-01-23 15:45:52 +03:00
|
|
|
|
2024-01-22 17:08:45 +03:00
|
|
|
$page = file_get_contents($this->url);
|
2024-01-23 15:45:52 +03:00
|
|
|
$strForPregMatch = "/" . "{$this->contentMarker}" . "(.*)<\/div>/is";
|
|
|
|
|
2024-01-22 17:08:45 +03:00
|
|
|
$arr = [];
|
|
|
|
$rez = preg_match_all($strForPregMatch, $page, $arr);
|
2024-01-23 15:45:52 +03:00
|
|
|
|
2024-01-26 16:05:28 +03:00
|
|
|
return $content = $arr[0][0];
|
2024-01-22 17:08:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function normalizeURLFile($content)
|
|
|
|
{
|
2024-01-24 13:30:27 +03:00
|
|
|
|
2024-01-25 08:59:34 +03:00
|
|
|
$rez = preg_match_all('/<a href="(.*)">/isU', $content, $arr);
|
2024-01-24 13:30:27 +03:00
|
|
|
$arr[1] = array_unique($arr[1]);
|
2024-01-22 17:08:45 +03:00
|
|
|
foreach ($arr[1] as $el) {
|
2024-01-25 08:59:34 +03:00
|
|
|
if (!str_starts_with($el, 'https')) {
|
|
|
|
$content = str_replace($el, 'https://mkgtu.ru' . $el, $content);
|
2024-01-22 17:08:45 +03:00
|
|
|
}
|
|
|
|
}
|
2024-01-25 12:57:38 +03:00
|
|
|
$rez = preg_match_all('/src="(.*)">/isU', $content, $arr);
|
|
|
|
$arr[1] = array_unique($arr[1]);
|
|
|
|
foreach ($arr[1] as $el) {
|
2024-01-29 09:44:53 +03:00
|
|
|
if (!str_starts_with($el, 'https') && str_contains($el, 'upload')) {
|
2024-01-25 12:57:38 +03:00
|
|
|
$content = str_replace($el, 'https://mkgtu.ru' . $el, $content);
|
|
|
|
}
|
|
|
|
}
|
2024-01-24 15:23:23 +03:00
|
|
|
|
2024-01-22 17:08:45 +03:00
|
|
|
return $content;
|
2024-01-24 13:30:27 +03:00
|
|
|
}
|
2024-01-25 08:59:34 +03:00
|
|
|
public function cutHTML($content, $strForScissors)
|
2024-01-24 13:30:27 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
$arr = [];
|
2024-01-25 14:01:05 +03:00
|
|
|
//<footer(.*)<\/footer>
|
|
|
|
//safdsaf sdfdasf<footer>--------------------------------fsdfdasf <\/footer> asdfdasf asdf
|
2024-01-24 13:30:27 +03:00
|
|
|
$rez = preg_match_all($strForScissors, $content, $arr);
|
2024-01-25 14:01:05 +03:00
|
|
|
//$arr[1][0] = '>--------------------------------fsdfdasf ';
|
|
|
|
$content = str_replace($arr[0], '', $content);
|
|
|
|
//safdsaf sdfdasf<footer<\/footer> asdfdasf asdf
|
2024-01-24 13:30:27 +03:00
|
|
|
|
|
|
|
return $content;
|
2024-01-22 17:08:45 +03:00
|
|
|
}
|
|
|
|
}
|