2014-09-25 30 views
0

我試圖修改腳本提取信息:提取的信息與PHP preg_match_all

存在與示例頁面:

<ul class="tags"> 
<li><div class="tag"><a href="http://www.url1.com" title="url1">title 1</a></div></li> 
<li><div class="tag"><a href="http://www.url2.com" title="url2">title 2</a></div></li> 
</ul> 

我想獲得標題1,標題2

我有這樣的代碼:

$m=array(); 
preg_match_all("/<a href="(.*)" title="(.*)">(.*)<\/a>/i", $buff,$m); 
$info['tags']=trim(strip_tags($m[3])); 
$cats=array_map('trim',$cats); 
$info['tags']=implode(',',$cats); 

,但我得到與裝飾和array_map錯誤。

感謝您的幫助。

+0

我建議使用這是一個HTML解析器,'DOMDocument'特別是 – Ghost 2014-09-25 12:25:00

回答

0

代碼中的第一個錯誤是在第三行:

$info['tags']=trim(strip_tags($m[3])); 

這裏功能strip_tags應給予字符串作爲參數,但$m[3]是數組。速戰速決是:

$info['tags']=array_map('strip_tags',array_map('trim',$m[3])); 

$信息[標籤]現在是:

[tags] => Array 
    (
     [0] => title 1 
     [1] => title 2 
    ) 

比申請破滅功能,所以:

$info['tags'] = implode(',', $info['tags']); 

乾杯,衣

+0

我希望$ info [標籤]是標題1,標題2 – 2014-09-25 12:38:24

+0

在這種情況下喲你可以使用implode函數:$ info [tags] = implode(「,」,$ info [tags]),結果將是標題1,title2 – bearea 2014-09-25 19:37:52