使用特定ID获取src的Web Scraping

I'm trying to obtain the src of a with a specific ID. Example:

<img id="hi_1" src="url of image 1">
<img id="hi_2" src="url of image 2">
<img id="hi_3" src="url of image 3">

result = url of image 1;

I have this code:

$html = file_get_contents('url of site');
preg_match('here I don't know what to do', $html, $src);
$src_out = $src[1];

You're looking for something like <img id="hi_1" src="(.*)">, but regex isn't the right way to go about this. Try using the DOM as per other answers to this question.

This will solve your problem :)

More information you will find in php documentation.

<?php

    $html = '<img id="hi_1" src="url of image 1">
    <img id="hi_2" src="url of image 2">
    <img id="hi_3" src="url of image 3">';


        $dom = new domDocument();
        $dom->loadHTML($html);
        $dom->preserveWhiteSpace = false;
        $images = $dom->getElementsByTagName('img');
        foreach ($images as $image) {
            $img_id =  $image->getAttribute('id');

            if($img_id == 'hi_2') {
                echo $image->getAttribute('src');

            }
        }