0

人们上网并提交一篇带有标题和描述的文章。人们经常包含链接,但它们显示为基本文本。我想要一种方式,当人们包含一个 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>";

}
4

2 回答 2

0

$reg_exUrl没有在任何地方回响。所以,你a href没有出现

于 2012-10-12T18:13:02.377 回答
0

这是因为您在替换为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_ 函数。

于 2012-10-12T18:11:35.430 回答