-1

I managed to extract the source code of a website, and now I need to find the URL to a specific image in the source. The source I retrieved is in a string format, stored in $query.

The format the image comes in is as follows:

<img src="#####500x500.jpg" width="500" height="500" alt="#####" itemprop="#####"> 

The ##### means that part of the code is different per URL I use. If I open the source code of the page in my web browser (Chrome: right click, view page source) and I search for 500x500 myself, I get 2 results. The second result is the image I'm looking for, and I want to grab the src attribute of that image.

So as an example: I enter a URL, the code I already have gets the source in a string format, and somewhere inside that string is the following:

<img src="https://example.com/hello500x500.jpg" width="500" height="500" alt="random-alt" itemprop="random-prop">

Now I want to store the src attribute in a variable.

Basically, in the end I get the following variable:

$variablename = 'https://example.com/hello500x500.jpg';
BSMP
  • 4,596
  • 8
  • 33
  • 44
Megarobo
  • 3
  • 4

1 Answers1

-1

You can use a regexp for that, try something like

$count = preg_match_all('/<img src=(\'|")(.*500x500\.jpg)/i', $content, $m);

The matches are stored in $m, use var_dump($m) to inspect the contents

Otto
  • 879
  • 9
  • 7