所以我今天正在学习正则表达式和其他东西,我让它按照我想要的方式工作,但是当我试图把它用作从网站获取数据的一部分时,它不起作用。有趣的是,如果我回显了我正在检索的原始 html,然后手动将其作为字符串放入正则表达式中,它就可以正常工作。有谁知道为什么会发生这种情况?这是代码:
preg_match_all('/<img src="images\/cms\/trinket\/(.*).png" \/><\/a>
<div style="width:85px; font-size:14px; font-weight:bold;
color:#731c08;">(.*)<\/div>/iU', snd('test.php'),
$matches, PREG_SET_ORDER);
snd 是 curl 返回可能导致问题的数据的函数,但我不知道。
snd函数:
function snd($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://whatever.com/'.$url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1468.0 Safari/537.36');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
示例(为便于阅读而添加了换行符):
<div style="width:600px; margin-left:auto; margin-right:auto; text-align:center; position:relative; z-index:1;">
<span style="display:inline-block; width:85px; height:100px;">
<a rel="includes/itemajax.php?id=789&tab=trinket" class="clue">
<img src="images/cms/trinket/789.png" /></a>
<div style="width:85px; font-size:14px; font-weight:bold; color:#731c08;">4</div>
</span> <span style="display:inline-block; width:85px; height:100px;">
<a rel="includes/itemajax.php?id=891&tab=trinket" class="clue">
<img src="images/cms/trinket/891.png" /></a>
<div style="width:85px; font-size:14px; font-weight:bold; color:#731c08;">3</div> </span>
应该返回:789,4 和 891,3