如何从数据库中获取URL

时间:2014-02-22 16:50:05

标签: php html database mysqli

所以我有三个页面,一个是索引页面。将数据从索引页面内的表单写入数据库。还有一个从数据库中获取数据并回显一个带有数据的html表。

目前,如果您在表单中编写链接。它将作为文本出现。我希望整个链接都像<a href="[link]">[link]</a>

所以说如果我把它写在表格上:

请看:www.google.com 请看:https://www.google.com

它会在html

中出现

请看:<a href="www.google.com">www.google.com</a>

我怎么能这样做?


好的,所以html是:

<form class="wide" action="Write-to.php" method="post">
        <input class="wide" autocomplete="off" name="txt" type="text" id="usermsg" style="font-size:2.4vw;" value=""  />
</form>

用户会写:

“看看这个:www.google.com 看看这个:https://www.google.com

然后通过Write-to.php将其发送到数据库。

$sql="INSERT INTO social (comunicate)

VALUES

('$_POST[txt]')";
}

然后将其写回数据库:

$result = mysqli_query($con,"(select * from social order by id desc limit {$limit_amt}) order by id asc");
while($row = mysqli_fetch_array($result))
{
      echo "<tr div id='".$i."' class='border_bottom'>";
      echo "<th scope='col'>";
      echo "<span class='text'>".htmlspecialchars($row['comunicate'])."</span><br />";
      echo "</th>";
      echo "</tr>";
 }

4 个答案:

答案 0 :(得分:1)

试试吧:

echo('<a href="'.$your_url_variable.'">'.$your_url_variable.'</a>');

更新

OP真的想要检测字符串中的url。一种可能的解决方案是使用正则表达式过滤它。这段代码可以提供帮助:

<?php

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

// The Text you want to filter for urls
$text = "The text you want to filter goes here. http://google.com";

// Check if there is a url in the text
if(preg_match($reg_exUrl, $text, $url)) {

       // make the urls hyper links
       echo preg_replace($reg_exUrl, "<a href="{$url[0]}">{$url[0]}</a> ", $text);

} else {

       // if no urls in the text just return the text
       echo $text;

}
?>

来源:http://css-tricks.com/snippets/php/find-urls-in-text-make-links/

答案 1 :(得分:1)

在显示用户提供的(受污染的)数据时,您需要担心很多事情。

function validateUrl($url) {
    return filter_var($url, FILTER_VALIDATE_URL); 
}
  • 您尝试做的是将字符串转换为链接,例如您可以编写如下函数:
function makeClickable($link) {
    $link = htmlspecialchars($link);
    return sprintf('<a href="%s">%s</a>', $link, $link);
}
  • 您也可以使用字符串连接,但我不会在视图代码中执行此操作。只是个人偏好。

  • 看看urlencode功能,它肯定会派上用场。

  • 我还建议您阅读cross site scripting

请注意,我没有提出任何实施建议,我的目的只是向您展示一些人为设计的代码示例,以证明字符串是“可点击的”。

更新: 如果您想在文本中单击链接,请参阅以下问题:

答案 2 :(得分:0)

将db中的超链接保存并通过sql查询检索为字符串 喜欢:

select link from table_name where index = i 并将链接保存为:<a href="whatever"> whaatever here</a>

并打印

答案 3 :(得分:0)

Use this
echo '<a href="' . $res['url'] . '">' . $res['url'] . '</a>';
相关问题