Php 从Mysql行提取URL
人们上网并提交一篇带有标题和描述的文章。人们通常会包含链接,但它们显示为基本文本。我想要一种方式,当人们包括一个网址,它被认为是一个工作链接。我已经写了一段代码,但它只扫描一行,在实际的表中似乎不起作用,因为它在表外回响 基本上…我想要一个表,当一个链接被提交时,一个超链接被制作。有什么想法吗? 下面更新的代码保持了同样的情况 我的代码如下:Php 从Mysql行提取URL,php,Php,人们上网并提交一篇带有标题和描述的文章。人们通常会包含链接,但它们显示为基本文本。我想要一种方式,当人们包括一个网址,它被认为是一个工作链接。我已经写了一段代码,但它只扫描一行,在实际的表中似乎不起作用,因为它在表外回响 基本上…我想要一个表,当一个链接被提交时,一个超链接被制作。有什么想法吗? 下面更新的代码保持了同样的情况 我的代码如下: $query = "SELECT * FROM rumours ORDER BY id DESC"; $query = 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>";
}
$query=“按id描述从谣言订单中选择*”;
$query=mysql\u query($query)或die('mysql查询错误:'.mysql\u错误($connect));
while($row=mysql\u fetch\u assoc($query)){
$id=$row['id'];
$band=$row['band'];
$title=$row['title'];
$description=$row['description'];
$reg|u exUrl=“/(http | https | ftp | ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/”;
//要筛选URL的文本
//检查文本中是否有url
if(preg_match($reg_exUrl,$description,$url)){
//使URL成为超链接
preg_replace($reg_exUrl,,$description);
}
回声“;
回声“;
回音“$title”;
回声“;
回声“;
回显“$description”;
回声“;
回声“;
}
这是因为在使用preg\u 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>";
}
$query=“按id描述从谣言订单中选择*”;
$query=mysql\u query($query)或die('mysql查询错误:'.mysql\u错误($connect));
while($row=mysql\u fetch\u assoc($query)){
$id=$row['id'];
$band=$row['band'];
$title=$row['title'];
$description=$row['description'];
$reg|u exUrl=“/(http | https | ftp | ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/”;
//要筛选URL的文本
//检查文本中是否有url
if(preg_match($reg_exUrl,$description,$url)){
//使URL成为超链接
preg_replace($reg_exUrl,,$description);
}
回声“;
回声“;
回音“$title”;
回声“;
回声“;
回显“$description”;
回声“;
回声“;
}
顺便说一句,试着用PDO或mysqli_uu代替常规的mysql_u函数。
不会在任何地方重复出现。因此,您的a href
没有出现请参见以下问题: