2015-10-27 102 views
0
之間的串

我怎樣才能得到這個鏈接 「http://example.com/view.php?id=5841」 從代碼:PHP的preg_match返回兩個字符串

<h3 class="coursename"><a class="" href="http://example.com/view.php?id=521">D<span class="highlight">LAW</span> <span class="highlight">130</span>Management</a></h3><div class="moreinfo"></div></div><div class="content"><ul class="teachers"><li>Teacher: <a href="http://example.com/">John</a></li></ul><div class="coursecat">Category: <a class="" href="http://example.com/">First</a></div></div></div><div class="coursebox clearfix even" data-courseid="5841" data-type="1"><div class="info"><h3 class="coursename"><a class="" href="http://example.com/view.php?id=5841"><span class="highlight">LAW</span> <span class="highlight">130` 

我想:

preg_match('/href="(.*)"><span class="highlight">LAW/isU',$BBB,$AAA); 

,結果是:

http://example.com/view.php?id=521">D<span class="highlight">LAW</span> <span class="highlight">130</span>Management</a></h3><div class="moreinfo"></div></div><div class="content"><ul class="teachers"><li>Teacher: <a href="http://example.com/">John</a></li></ul><div class="coursecat">Category: <a class="" href="http://example.com/">First</a></div></div></div><div class="coursebox clearfix even" data-courseid="5841" data-type="1"><div class="info"><h3 class="coursename"><a class="" href="http://example.com/view.php?id=5841 
+0

做你想做的所有鏈接,或與ID鏈接或與particullar ID,如果你希望所有那麼這就夠了'preg_match('href =「(。*?)」',$ data,$ res);' ' –

回答

0

用此代替:

/href="(.[^<]*?)"><span class="highlight">LAW/isU 

這是一種告訴正則表達式找到與你想要的最短表達式相匹配的簡單方法。

+0

謝謝你的工作。 – Akram

0

使用XPath查詢:

libxml_use_internal_errors(true); 
$dom = new DOMDocument; 
$dom->loadHTML($yourHTML); 

$xp = new DOMXPath($dom); 
$link = $xp->query('//a[span[@class="highlight"]][starts-with(.,"LAW")][1]/@href')->item(0)->nodeValue; 

echo $link; 

查詢詳情:

// # axe: anywhere in the DOM tree 
a # axe: a "a" tag 
[span[@class="highlight"]] # predicate: the "a" tag has for direct child a "span" tag 
          # with a "class" attribute equal to "highlight" 
[starts-with(.,"LAW")]  # predicate: its text content begins with "LAW" 
[1]      # predicate: first occurrence (no need to search another one) 
/@href      # axe: its "href" attribute 
+0

謝謝,我是需要PHP,但謝謝:) – Akram

+0

@ Akk:它是PHP;) –

+0

我是初學者程序員,我不知道如何使用XPath查詢>>反正非常感謝你 – Akram