2009-10-05 139 views
2

Hello i want to extract links <a href="/portal/clients/show/entityId/2121" > and i want a regex which givs me /portal/clients/show/entityId/2121 the number at last 2121 is in other links different any idea?Preg_match_all <a href

+0

你想使用正則表達式從'/ portal/clients/show/entityId/2121'中提取'2121'嗎? – halocursed 2009-10-05 12:11:00

+0

不,我想提取'/門戶/客戶端/顯示/ entityId/2121' 另一個鏈接可以有不同的數字,而不是2121任何想法? – streetparade 2009-10-05 12:13:19

回答

0

正則表達式解析鏈接是這樣的:

'/<a\s+(?:[^"'>]+|"[^"]*"|'[^']*')*href=("[^"]+"|'[^']+'|[^<>\s]+)/i' 

既然是多麼的可怕,我會建議使用Simple HTML Dom至少得到鏈接。然後你可以在鏈接href中使用一些非常基本的正則表達式來檢查鏈接。

+0

@streetparade您可能希望避免在捕獲的值中包含引用屬性值的引號,因此,請相應地調整正則表達式捕獲相關: '/ ] + | 「[^」] * 「| \ '[^ \'] * \')* HREF = 」([^「] +)」 | \ '[^ \'] + \'| [^ <> \ s]的+/I」 – 2014-08-28 16:56:32

9

Simple PHP HTML Dom Parser例如:

// Create DOM from string 
$html = str_get_html($links); 

//or 
$html = file_get_html('www.example.com'); 

foreach($html->find('a') as $link) { 
    echo $link->href . '<br />'; 
} 
+0

這會給結果「 – streetparade 2009-10-05 12:26:21

+0

但我只是提取/門戶/客戶端/顯示/ entityId/4636所以這工作 '/ ] + |」[^「] *」|'[^'] *' )* href =(「[^」] +「|'[^'] +'| [^ <> \ s] +)/ i' – streetparade 2009-10-05 12:26:57

+0

@streetparade my bad,忘記說$ link-> href,編輯 – karim79 2009-10-05 12:30:13

4

Don't use regular expressions for proccessing xml/html。這可以很容易地使用來完成的builtin dom parser

$doc = new DOMDocument(); 
$doc->loadHTML($htmlAsString); 
$xpath = new DOMXPath($doc); 
$nodeList = $xpath->query('//a/@href'); 
for ($i = 0; $i < $nodeList->length; $i++) { 
    # Xpath query for attributes gives a NodeList containing DOMAttr objects. 
    # http://php.net/manual/en/class.domattr.php 
    echo $nodeList->item($i)->value . "<br/>\n"; 
} 
0

這是我的解決方案:

<?php 
// get links 
$website = file_get_contents("http://www.example.com"); // download contents of www.example.com 
preg_match_all("<a href=\x22(.+?)\x22>", $website, $matches); // save all links \x22 = " 

// delete redundant parts 
$matches = str_replace("a href=", "", $matches); // remove a href= 
$matches = str_replace("\"", "", $matches); // remove " 

// output all matches 
print_r($matches[1]); 
?> 

我建議避免使用基於XML解析器,因爲你不會總是知道, 文檔是否/網站已經形成良好。

祝你好運