2012-10-12 44 views
0

人們上網並提交一篇文章,標題和說明。人們通常會包含鏈接,但它們顯示爲基本文本。我想要一種方式,當人們包含一個url時,它被認爲是一個工作鏈接。我寫了一段代碼,但它只掃描一行,似乎在實際的表中不起作用,因爲它在表外回顯。從Mysql行中提取網址

基本上...我想要一個表格,當鏈接被提交時,超鏈接被創建。有任何想法嗎? 下面更新後的代碼會保持相同的狀態。

我的代碼如下:

 $query = "SELECT * FROM rumours ORDER BY id DESC"; 
    $query = mysql_query($query) or die('MySQL Query Error: ' . mysql_error($connect)); 
    while ($row = mysql_fetch_assoc($query)) { 
$id = $row['id']; 
$band = $row['band']; 
$title = $row['Title']; 
$description = $row['description']; 

$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/"; 

// The Text you want to filter for urls 
// Check if there is a url in the text 
    if(preg_match($reg_exUrl, $description, $url)) { 
    // make the urls hyper links 
    preg_replace($reg_exUrl, '<a href="'.$url[0].'">'.$url[0].'</a>', $description); 

     } 

    echo "<table border='1'>"; 
    echo "<tr>"; 
    echo "<td> $title </td>"; 
    echo "</tr>"; 
    echo "<tr>"; 
    echo "<td class = 'td1'> $description </td>"; 
    echo "</tr>"; 
    echo "</table>"; 

} 
+0

看到這個問題:http://stackoverflow.com/questions/1188129/replace-urls-in-text-with-html-links – Ollie

回答

0

這是因爲你打印出來之前你在與preg_replace替換...

把你的預浸料的循環中那樣:

$query = "SELECT * FROM rumours ORDER BY id DESC"; 
$query = mysql_query($query) or die('MySQL Query Error: ' . mysql_error($connect)); 
while ($row = mysql_fetch_assoc($query)) { 
    $id = $row['id']; 
    $band = $row['band']; 
    $title = $row['Title']; 
    $description = $row['description']; 

    $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/"; 

    // The Text you want to filter for urls 

    // Check if there is a url in the text 
    if(preg_match($reg_exUrl, $description, $url)) { 
      // make the urls hyper links 
      preg_replace($reg_exUrl, '<a href="'.$url[0].'">'.$url[0].'</a>', $description); 

    } 

    echo "<table border='1'>"; 
    echo "<tr>"; 
    echo "<td> $title </td>"; 
    echo "</tr>"; 
    echo "<tr>"; 
    echo "<td class = 'td1'> $description </td>"; 
    echo "</tr>"; 
    echo "</table>"; 

} 

順便說一句,嘗試使用PDO或mysqli_,而不是常規的mysql_函數。

+0

沒有改變的代碼全部 –

0

$reg_exUrl沒有在任何地方回顯。所以,你的a href沒有出現

+0

我會在哪裏迴應它? –