簡體   English   中英

從Mysql行中提取Urls

[英]Extract Urls from Mysql Row

人們上網並提交帶有標題和描述的文章。 人們經常包括鏈接,但它們顯示為基本文本。 我想要一種當人們包含URL時將其識別為有效鏈接的方法。 我已經編寫了一個代碼,但是它只掃描一行並且在實際表中似乎不起作用,因為它在表外回顯。

基本上...我想要一個表,在該表中提交鏈接時,將創建超鏈接。 有任何想法嗎? 下面的更新代碼使相同的事情繼續進行。

我的代碼如下:

      $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>";

}

這是因為在執行preg_replace替換之前,您先打印出了...

像這樣將您的preg放入循環中:

$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_函數。

$reg_exUrl在任何地方都沒有回顯。 因此,您a href並未出現

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM