-5

I have following html code

<img src="https://example.com/123456789/300.jpg?key=abcdefg" />
<img src="https://example.com/123456789/600.jpg?key=abcdefg" />
<img src="https://example.com/123456789/900.jpg?key=abcdefg" />
<img src="https://example.com/123456789/1200.jpg?key=abcdefg" />

i want to get the whole string contains 600.jpg Output should be

https://example.com/123456789/600.jpg?key=abcdefg

Dom phrasing is not allowed while scraping page, because it is blocked.

David Corp
  • 496
  • 7
  • 14
  • i am also trying to move left from 600.jpg to first left double quote, and then 600.jpg to first right double quote, but not getting how to move left. – David Corp Jun 18 '18 at 03:05
  • I'm not sure what you mean by `move`, aren't you just looking for the 600.jpg image? – user3783243 Jun 18 '18 at 03:11
  • Downvotes are ment for bad questions. Not for questions you don't know the answer to. A bad question is a question that starts with _"How do I..?"_. A good question is _"I've tried this, it didn't work, what did I do wrong?"_ – icecub Jun 18 '18 at 03:13
  • Thanks, i understand, i edit my question now. – David Corp Jun 18 '18 at 03:21

1 Answers1

2

A regex is not the correct tool for this. Use a parser,

$string = '<img src="https://example.com/123456789/300.jpg?key=abcdefg" />
<img src="https://example.com/123456789/600.jpg?key=abcdefg" />
<img src="https://example.com/123456789/900.jpg?key=abcdefg" />
<img src="https://example.com/123456789/1200.jpg?key=abcdefg" />';
$dom = new DOMDocument;
$dom->loadHTML($string);
$images = $dom->getElementsByTagName('img');
foreach($images as $image) {
    echo $image->getAttribute('src') . PHP_EOL;
}

https://3v4l.org/GOo3R

Also see:

http://php.net/manual/en/domdocument.getelementsbytagname.php
http://php.net/manual/en/domelement.getattribute.php

Update:

To look for a particular bit strpos or preg_match could be used:

$string = '<img src="https://example.com/123456789/300.jpg?key=abcdefg" />
<img src="https://example.com/123456789/600.jpg?key=abcdefg" />
<img src="https://example.com/123456789/900.jpg?key=abcdefg" />
<img src="https://example.com/123456789/1200.jpg?key=abcdefg" />';
$dom = new DOMDocument;
$dom->loadHTML($string);
$images = $dom->getElementsByTagName('img');
foreach($images as $image) {
    if(strpos($image->getAttribute('src'), '600.jpg') !== FALSE) {
        echo $image->getAttribute('src') . PHP_EOL;
    }
}

https://3v4l.org/IFok0

user3783243
  • 5,368
  • 5
  • 22
  • 41
  • i need it without DOMDocument because dom phraser disabled while loading page, but thanks for your hard work. – David Corp Jun 18 '18 at 03:12
  • 1
    What do you mean `while loading page`? Are you doing this with JS? Please show your actual usage. If you can use `preg_match` I don't see why the parser is unavailable. – user3783243 Jun 18 '18 at 03:16
  • No, i am doing with php but the site i am scraping not allow dom phrasing. – David Corp Jun 18 '18 at 03:18
  • 1
    @DavidCorp Perhaps you should edit your question and clearly explain what it is you're trying to do. I mean like, what your project is. That helps prevent answers that are of no use to you. – icecub Jun 18 '18 at 03:19
  • 1
    It doesn't matter what the other site has on it. When you get this string to your PHP you should be able to use the parser. If you have `` in the PHP you can use this answer. If you don't please edit the question to show what you have and what you are doing. – user3783243 Jun 18 '18 at 03:22
  • ok problem solved – David Corp Jun 18 '18 at 03:38
  • @DavidCorp Did this answer solve it or did you use that regex answer? – user3783243 Jun 18 '18 at 03:39
  • i solved it by preg_match_all here is example https://3v4l.org/sa7bY , but too down votes. Thanks for your support. I upvoted you and marked your answer as solved. – David Corp Jun 18 '18 at 03:52
  • 1
    I don't see why you can't use the parser. If you have `$string` you should be able to use the paser. The other sites technology doesn't matter (unless it were blocking you, but in that case `$string` wouldn't populate). You don't need to accept an answer, accepted answers should only be for answers that resolved your issue. If the other answer is what worked you should upvote that and unaccept this. However, as I've stated, I can't see why this wouldnt work. – user3783243 Jun 18 '18 at 03:57