2011-09-30 46 views
3

如何檢測文本和標題中是否有任何URL(如果有)?檢測 - URL的標題和URL

如果有一個的話,那應該改變的網址:

來自:http://stackoverflow.com

到:

<detected:url="http://stackoverflow.com"/> 

我也需要檢索這樣的例子外部鏈接標題:

<title:http://stackoverflow.com/="the actual title from the stackoverflow"/> 
+0

這取決於。所有網址是否都以「http?」開頭什麼表示網址的標題? –

+0

@tandu不是因爲它會解析用戶發佈的數據。 標題是從標籤檢測到的網站網址標題。 – Lucas

+0

這應該是XML嗎?如果是這樣,它不是有效的XML。 – icktoofay

回答

1

這是爲單個URL的情況:

$url = "http://www.stackoverflow.com/"; 

$check_result = get_detected_and_title($url); 


function get_detected_and_title($url) 
{ 
    $detected = '<detected:url="'.$url.'"/>'; 
    $title = ''; 
    $tmp_html = file_get_contents($url); 
    preg_match('/<title>(.*)<\/title>/', $tmp_html, $res); 
    $title = '<title:'.$url.'="'.$res[1].'"/>'; 

    return array($detected, $title); 
} 

其實,通過這麼的網頁看後,我覺得這是更接近你在找什麼。雖然它需要一些調整:How to mimic StackOverflow Auto-Link Behavior