PHP teaches you how to extract image links in img tags, as well as src and alt attribute values

Sometimes we have such a need to extract the entire img tag in a series of html pages, or extract the src attribute in the img tag, which is the image link we need, and paste the code directly without further ado. code show as below:

function extract_img($tag) {
    
    
    preg_match_all('/(id|alt|title|src)=("[^"]*")/i', $tag, $matches);
    $ret = array();
    foreach($matches[1] as $i => $v) {
    
    
        $ret[$v] = $matches[2][$i];
    } 
    return $ret;
}
 
$img_tag = '<img id="logo" title="我要吃鱼啊" src="http://www.baidu.net/wp-content/uploads/2015/06/2015-06-02.jpg" alt="我爱吃鱼" />';
 
$atts = extract_img($img_tag);
print_r($atts);

The following is the return result after program execution :

返回结果

Array
(
    [id] => "logo"
    [src] => "http://www.devdo.net/wp-content/uploads/2015/06/2015-06-02.jpg"
    [alt] => "我爱吃鱼"
    [title] => "我爱吃鱼啊"
)

The following is the code for my own use, the purpose is to grab the pictures of a certain picture site, but the site owner has done some processing on the picture collection, but how can this processing method be difficult for programmers?

//需要提取的网页代码
$str='
			';
function getSimpleImg($str = '')
{
    
    
     if (!empty($str)) {
    
    
        $result = preg_replace("/.*<img[^>]*srcset[=\s\"\']+([^\"\']*)[\"\'].*/", "$1", $str);
     } else {
    
    
        $result = '';
     }  
}

getSimpleImg($str);

function extract_attrib($tag) {
    
    
    preg_match_all('/(srcset)=("[^"]*")/i', $tag, $matches);
   // print_r($matches[2]);
    $ret = array();
    $img='';
    foreach($matches[2] as $i => $v) {
        if ($i%2==0) {
          //$img.="<img src='$v' />"."<br>"; 
          $v= str_replace("-200x200","",$v);
          $v=str_replace("-300x300","",$v);
          echo '<img src='."$v" .'/>';
        } 
        
    } 
    return $img;
}
 
$atts = extract_attrib($str);
print_r($atts);

Guess you like

Origin blog.csdn.net/t1174148618/article/details/106610214