Answer the question
In order to leave comments, you need to log in
How to parse photos using preg_match_all?
Hello everyone
, I have a code that parses images from a site using a link
<?php
// страница с картинками
$text = file_get_contents("ссылка на сайт для парсинга");
// выцеливаем путь к картинкам и помещаем их в массив
preg_match_all("'<img\s+src=\"(\S*.(png|jpg))\"'si", $text, $result);
echo"Найдено картинок на странице = ".count($result[1]);
//print_r($result[1]); // найденные картинки
echo("<br>");
echo("<br>");
echo("Urls - картинок...");
// создаём папку если таковой нет
if (!file_exists("images"))
{
mkdir("images", 0700); // создаём папку
}
$move_dir = "images/"; // Директория созданной папки
for($i = 0; $i <=(count($result[1])-1); $i++)
{
// формируем урл на картинку
$url = "http://www.site.com/".$result[1][$i];
echo("<br>");
echo($url);
echo("<br>");
$filename = basename($url); // Имя картинки
file_put_contents($move_dir.'/'.$filename, file_get_contents($url));
}
echo("<br>");
echo("Копирование завершено!");
?>
<figure itemprop="associatedMedia" itemscope="" itemtype="http://schema.org/ImageObject">
<a data-w="174" data-h="250" class="item item-gallery" href="http://www.site.com/get_image/2/f4f6ea5666f7319419d4436374de951b/main/1920x1920/10000/10423/1152485.jpg/" itemprop="contentUrl" data-size="1280x1920" style="width: 154px; height: 220px; display: block;">
<img src="http://www.site.com/contents/albums/main/370x250/10000/10423/1152485.jpg" itemprop="thumbnail" alt="">
</a>
</figure>
Answer the question
In order to leave comments, you need to log in
here
preg_match_all("'<img\s+src=\"(\S*.(png|jpg))\"'si", $text, $result);
"'<img\s+src=\"(\S*.(png|jpg))\"'si"
<a>
href="http://www.site.com/get_image/2/f4f6ea5666f7319419d4436374de951b/main/1920x1920/10000/10423/1152485.jpg/
href=".*([a-z\_\-\.])*\.((jpg)|(png))\/"
href="
and "
with an empty string ""
and you have a clean url at the output.
Try in the control panel "Settings" -\u003e "Permalinks" select some other type of display links and save.
Update duplicator. In one of the latest versions, there was a bug that changed the % symbol in the database to this kind of rubbish. With the update of the plugin, this problem has disappeared for me personally.
solution for an already installed site with a bug:
1. download the database to a sql file, open it with an editor
2. do a search / replace from
{03873571dc18fad47add251c551321dbad75fc58166b9b4f6f1c1bdbb6ac251}
to
%
3. save, upload the database.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question