R
R
Renat Sagdeev2014-11-09 22:33:04
PHP
Renat Sagdeev, 2014-11-09 22:33:04

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>';
  }
?>

62942f21eed444f68303f3bff7eebfbc.png
But how to break them into local ones and no, I can’t. For example in two different div'a.
I am not strong in php.
Who will tell?
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Surin, 2014-11-10
@feyak1n

<?
    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>';
        }
    }
?>

Will output only local elements.

O
OlegLazarenko, 2014-11-09
@OlegLazarenko

<?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>";
?>

Didn't check

S
Sergey, 2014-11-09
@zetabit

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 question

Ask a Question

731 491 924 answers to any question