2016-02-17 23 views
0

这是我的网址:我如何得到preg_match_all();从URL

<a href="http://sbayjai.com/forum/index.php?topic=67433.0">test_curl</a> 

我想用preg_match_all();获得

http://sbayjai.com/forum/index.php?topic=67433.0 

谢谢你的帮助。

回答

0

您可以使用下面的正则表达式:

$text = '<a href="http://sbayjai.com/forum/index.php?topic=67433.0">test_curl</a>'; 
$regex = '<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>'; 

if(preg_match_all("/$regex/siU", $text, $matches)) { 
    // urls 
    var_dump($matches[2]); 
    // link text 
    var_dump($matches[3]); 
} 

输出:

array(1) { 
    [0]=> 
    string(48) "http://sbayjai.com/forum/index.php?topic=67433.0" 
} 
array(1) { 
    [0]=> 
    string(9) "test_curl" 
} 

正则表达式是从http://www.the-art-of-web.com/php/parse-links/拍摄,你可以找到所有的细节很好的解释,如果你有兴趣了解它是如何工作的。

+0

谢谢,我会试试看。 – sammy

+0

看我的答案。 – sammy

+0

你必须使用正确的正则表达式*/ * href =(\“??)([^ \>>] *?)\\ 1 [^>] *>(。*)<\/a> /'NOT'/ \ test_curl \ <\/a\> /' – maxhb