Answer the question
In order to leave comments, you need to log in
How to parse images using php?
Hello!
Please tell me how to solve the problem.
We need to list all the images from the page, and split them into two parts, which are located locally and on a different server. It turns out you need to check where the src attribute in the img tag begins, with http:// or not.
I found a parser and it works. With PHP Simple HTML DOM Parser.
Displays a list of all images.
<?php
include "simple_html_dom.php";
$html = file_get_html("http://ssau.ru");
foreach ($html -> find('img') as $element) {
echo $element-> src . '<br>';
}
?>
Answer the question
In order to leave comments, you need to log in
<?
include "simple_html_dom.php";
$html = file_get_html("http://ssau.ru");
foreach ($html -> find('img') as $element) {
if (strpos($element, "http") === false) {
echo $element-> src . '<br>';
}
}
?>
<?php
include "simple_html_dom.php";
$html = file_get_html("http://ssau.ru");
foreach ($html -> find('img') as $element) {
$imgType = $element->src[0] == '/' ? 'local' : 'remote';
$imgs[$imgType][] = $element->src;
}
echo 'Локальные картинки<br>';
foreach ($imgs['local'] as $img) echo "$img<br>";
echo 'Картинки с других серверов<br>';
foreach ($imgs['remote'] as $img) echo "$img<br>";
?>
well, there is a list of addresses, learn how to select a host from an address, come up with an algorithm (
there is an address, this is a string, you need to cut the host from the string and compare it with the local host,
for example, cut the host from the character position ["://" + 3 - because these are three characters] to the position "/" or to the end of the line [if there is no "/" character], you need to take into account that in the beginning before the substring "host" there are two more such characters "//"
and implement it, I advise you to look at the functions of working with strings in the php
documentation and for example, those lines that are without hosts (also determine this) are first considered immediately local ....
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question