2014-02-22 130 views
0

所以我有三個頁面是索引頁。將索引頁中的表單數據寫入數據庫的一種方法。還有一個從數據庫中獲取數據,並用數據回顯出一個html表格。如何從數據庫中獲取url

當前如果你在表格中寫一個鏈接。它會以文本形式出現。我希望整個鏈接像<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>"; 
} 
+0

您可以發佈您關於呼應URL編碼? – Fabio

+0

這只是使用mysqli'mysqli_fetch_array'的標準方式# – maxisme

+0

爲什麼downvote? – maxisme

回答

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/

+0

我知道如何創建鏈接!我只需要一種方法讓php識別它的一部分**是**鏈接 – maxisme

+1

使用正則表達式對其進行過濾。看看這個鏈接:[鏈接](http://css-tricks.com/snippets/php/find-urls-in-text-make-links/) – MillaresRoo

+0

正是我想要的! – maxisme

0

保存的超級鏈接在數據庫和檢索,通過SQL查詢 像一個字符串:

select link from table_name where index = i 並保存鏈接爲:<a href="whatever"> whaatever here</a>

並打印它

0
Use this 
echo '<a href="' . $res['url'] . '">' . $res['url'] . '</a>'; 
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

請注意,我沒有做任何落實的建議,我的目的就是向你展示,演示製作一個字符串「點擊」一些人爲的代碼示例。

更新: 如果你想使鏈接文本內點擊,請參考以下幾個問題:

+0

+1因爲有用的信息。但沒有真正回答這個問題。所以不知道我是否在這裏遵守法律 – maxisme

+0

我的「makeClickable」函數將使有效的用戶提供的鏈接可點擊,但有幾種情況需要照顧,例如您確定您總是會得到有效的網址嗎?有時用戶不會打擾http://等。我能做的最好的是給你一個簡單的演示,並且可以將它調整到你的用例:) – ymas