我试图使用两个preg_match
以从html
源代码中获取两个特定值。两个连续的preg_match
<?php
$url = "http://www.example.com";
$userAgent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1";
$ch = curl_init();
curl_setopt($ch,CURLOPT_USERAGENT,$userAgent);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_AUTOREFERER,true);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_TIMEOUT,10000000);
$html = curl_exec($ch);
preg_match('~<span class="first">(.*)<\/span>~msU',$html,$matching_data);
preg_match('~<span class="second">(.*)<\/span>~msU',$html,$matching_data2);
print_r($matching_data);
print_r($matching_data2);
?>
在这方面采取的$html
VAR包含以下顺序考虑:
<title>foobar title</title>
<body>
<div class="second">Not this one</span>
<div>
<span class="first">First</span>
<span class="second">this one<span>
</div>
</body>
如果我跑我php
代码,第一print_r
返回正确的价值:<span class="first">First</span>
。但第二个print_r
,而不是返回<span class="second">this one<span>
它返回<div class="second">Not this one</span>
。
所以我想preg_match
函数开始治疗的开始,而不是最后preg_match
调用。
如何让第二个(第三,第四等)呼叫preg_match
在最后一次呼叫时运行?
谢谢,
问候。
你可以使用preg_match_all。 – igorw 2011-02-08 13:05:58