我有網頁在約10-15鏈接存在,各個環節都在我的控制,並與像一些名人的話結束我想要篩選與字,所以我寫了這preg_match_all和正則表達式
結束所有鏈接$regex='|<a.*?href="(.*_celebrity)"|';
preg_match_all($regex,$result21,$parts);
$links=$parts[0];
foreach($links as $link){
{
echo $link."<br>";
mysql_query ("INSERT INTO tablea(linkssas) VALUES ('$link')");
}
它完成了工作並過濾了以_celebrity結尾的所有鏈接,但輸出沒有輸入到數據庫中。所有鏈接都輸入到一行中,並且它不是普通的,而是以錨文本的形式輸入,但我想要純文本鏈接在數據庫中,因爲我使用的是foreach,因此所有鏈接都應該輸入單獨的行,但所有行都以單行形式輸入,並以如下形式錨定:http://xyz.com/edje/jjeieied_celebrity">A</a>
但我只想要數據庫中的鏈接
你不應該使用正則表達式來獲取鏈接,而是使用DOMDocument。請閱讀:http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – Bgi
聽起來像一個貪婪的正則表達式的問題。真的你想'href =「(。*?_ celebrity)」',但_really_你最好使用適當的DOM解析器,如DOMDocument或SimpleXML。 –
這聽起來像一個工作託尼小馬.....或更好的是,[閱讀此](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self- contains-tags/1732454#1732454),這是一個很好的解釋,爲什麼你不應該嘗試使用正則表達式解析HTML。 – SDC