ownerDocument; $domElement->appendChild($domOwner->createCDATASection("{$value}")); } } public function addChildWithCData($name = '', $value = '') { $newChild = parent::addChild($name); if ($value) $this->addCDataToNode($newChild, "{$value}"); return $newChild; } public function addCData($value = '') { $this->addCDataToNode($this, "{$value}"); } } function modifyImageAttributes($html) { $html = preg_replace_callback('/]*)(width="(\d+)")([^>]*)(height="(\d+)")([^>]*)>/', function($matches) { $width = $matches[3]; $height = $matches[6]; $style = 'style="width: 100%; max-width: ' . $width . 'px; height: auto;"'; return ''; }, $html); return $html; } function removeStyles($html) { // Usuń atrybuty style i class $html = preg_replace_callback('/<(\w+)([^>]*)(\s*\/?>)/', function($matches) { if (strpos($matches[2], 'src') === false && strpos($matches[2], 'id="gallery"') === false && !($matches[1] === 'a' && strpos($matches[2], 'href') !== false)) { return '<' . $matches[1] . $matches[3]; } else { return '<' . $matches[1] . $matches[2] . $matches[3]; } }, $html); $html = preg_replace('/\r|\n/', '', $html); $html = preg_replace('/\s{2,}/', ' ', $html); do { $originalHtml = $html; $html = preg_replace('/<(\w+)\b[^>]*>\s*<\/\1>/', '', $html); } while ($originalHtml !== $html); do { $originalHtml = $html; $html = preg_replace('/
\s*
(.*?)<\/div>\s*<\/div>/', '
$1
', $html); } while ($originalHtml !== $html); return modifyImageAttributes( $html ); } function extractHrefsFromGallery($html) { $hrefs = []; $dom = new DOMDocument; @$dom->loadHTML($html); $xpath = new DOMXPath($dom); $galleryDiv = $xpath->query('//div[@id="gallery"]'); if ($galleryDiv->length > 0) { $anchors = $xpath->query('.//a', $galleryDiv->item(0)); foreach ($anchors as $anchor) { $href = $anchor->getAttribute('href'); if (!empty($href)) { $hrefs[] = $href; } } } return $hrefs; } function removeTagsWithSpecificIdsOrContent($html) { // Usuń tagi z id "testss", "gallery" lub "switch-effect" $html = preg_replace('/<(\w+)[^>]*\b(?:id="testss"|id="gallery"|id="switch-effect")[^>]*>.*?<\/\1>/', '', $html); // Usuń tagi, których zawartość to "Zobacz pełną listę aktualności" $html = preg_replace('/<(\w+)[^>]*>\s*Zobacz pełną listę aktualności\s*<\/\1>/', '', $html); // Usuń tagi, których zawartość to "Oryginalne zdjęcie" $html = preg_replace('/<(\w+)[^>]*>\s*Oryginalne zdjęcie\s*<\/\1>/', '', $html); // Usuń tagi