Regex & PHP - isolate src attribute from img tag

前端 未结 10 1455
眼角桃花
眼角桃花 2020-11-28 08:55

With PHP, how can I isolate the contents of the src attribute from $foo? The end result I\'m looking for would give me just \"http://example.com/img/image.jpg\"



        
10条回答
  •  伪装坚强ぢ
    2020-11-28 09:52

    If you don't wish to use regex (or any non-standard PHP components), a reasonable solution using the built-in DOMDocument class would be as follows:

    loadHTML('');
        $imageTags = $doc->getElementsByTagName('img');
    
        foreach($imageTags as $tag) {
            echo $tag->getAttribute('src');
        }
    ?>
    

提交回复
热议问题