[英]Extract Urls from Mysql Row
People go online and submit an article, with a title and description. 人们上网并提交带有标题和描述的文章。 And people often include links but they appear as basic text.
人们经常包括链接,但它们显示为基本文本。 I want a way in which when people include a url, it is recognized as a working link.
我想要一种当人们包含URL时将其识别为有效链接的方法。 I have written a code, but it only scans one row and doesn't seem to work in the actual table, as it is echoed outside of the table.
我已经编写了一个代码,但是它只扫描一行并且在实际表中似乎不起作用,因为它在表外回显。
Basically...i want a table where when a link is submitted, a hyperlink is made. 基本上...我想要一个表,在该表中提交链接时,将创建超链接。 Any ideas?
有任何想法吗? This updated code below keeps the same thing going on.
下面的更新代码使相同的事情继续进行。
My code is below: 我的代码如下:
$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>";
}
It's because you print out before you do the replace with preg_replace
... 这是因为在执行
preg_replace
替换之前,您先打印出了...
Put your preg inside your loop like that : 像这样将您的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>";
}
By the way, try using PDO or mysqli_ instead of regular mysql_ function. 顺便说一句,尝试使用PDO或mysqli_而不是常规的mysql_函数。
$reg_exUrl
isn't echoed anywhere. $reg_exUrl
在任何地方都没有回显。 So, your a href
isn't appearing 因此,您
a href
并未出现
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.