我想根据类或id获取图像中的src.防爆.在html页面上有很多<img src="url">但只有一个有类或id:
<img src="url" class="image" or id="image">
如何获得具有特定类或id的正确src属性?请正则表达式不是dom
我将解释你为什么我不想使用dom或其他库,因为我从其他站点获得一个不允许fopen或_file_get_contents或DOM的html页面,但只有Curl可以这样做.当然我有理由不使用像simplehtmldom这样的库,因为有时候不可能获得远程html页面,我应该自己制作一些脚本.
您说您不想使用DOM库,因为您需要使用cURL.这很好- DOMDocument并simple_xml_load_string都接受字符串参数.因此,您可以从cURL获取字符串并将其加载到DOM库中.
例如:
$html = curl_exec($ch); // assuming CURLOPT_RETURNTRANSFER
$dom = new DOMDocument;
$dom->loadHTML($html); // load the string from cURL into the DOMDocument object
// using an ID
$el = $dom->getElementById('image');
// using a class
$xpath = new DOMXPath($dom);
$els = $xpath->query('//img[@class="image"]');
$el = $els->item(0);
$src = $el->getAttribute('src');
Run Code Online (Sandbox Code Playgroud)