2011-10-11 93 views
1

我有一個表單提交併顯示文本。例如輸入http://www.twitter.com以明文形式顯示。我怎樣才能讓它自動顯示爲可點擊的鏈接?如何從數據庫顯示中獲取鏈接作爲可點擊的活動鏈接?

我正在使用MySQL和PHP。

回答

2

你propably希望你的腳本來自動檢測鏈接...我做了幾個星期前在一個腳本,從RSS提要推送推文...這是代碼。你可以用適當的變量替換$ _POST ['inputfield']!

if (preg_match('/www./',$_POST['inputfield'])) //checks if there's www. in the field. alternatively use http: instead of www. 
{ 
$link_start_pos = strpos($_POST['inputfield'],'www.'); //returns the link-start-pos, alternatively use http: instead of www. 

    // find the end of the link 
if($link_end_pos = strpos($_POST['inputfield'], ' ', $link_start_pos) === false) 
    { //if theres no space after the link, it's propably on the end of the text 
     $link_end_pos = strlen($_POST['inputfield']); 
    } 
    // form a string with the actual link in it 
$link = substr($_POST['inputfield'],$link_start_pos,$link_end_pos); 
    // and make it clickable 
$link_insert = '<a href="'.$link.'" target="_blank">'.$link.'</a>'; 
} 

希望幫助

+0

與我的解決方案是顛倒的,你可以使用strpos和$ link_end_pos和$ link_start_pos在實際鏈接之前/之前輸出文本:) – Anonymous

3

如果數據是一個網址,你可以把它包裝在一個標籤:

printf('<a href="%1$s">%1$s</a>', htmlspecialchars($your_link, ENT_QUOTES)); 

事情變得棘手,如果你需要將URL轉換成文本。大多數用戶不會因爲正確轉義鏈接而煩心,並且發佈example.com/Some Thing - [Me]而不是http://example.com/Some%20Thing%20-%20%5BMe%5D。查找a regular expression適合您的需求和用途:

echo preg_replace('#expression that matches a URL#', '<a href="$1">$1</a>', 
    htmlspecialchars($your_data, ENT_QUOTES)); 
+0

這是一個非常聰明的解決方案,雖然它在某種程度上以某種原因在我剛剛測試它的真實世界示例(Twitter提要)中以某種原因啓動' Anonymous

+0

只有'$ your_link'是一個URL時,第一個纔有效。第二個需要將數據分組,否則你會得到一個空的鏈接。 – Lekensteyn

0
$result = mysql_query('SELECT * FROM `links`'); 

if(mysql_num_rows($result)) { 
     foreach($row = mysql_fetch_object($result)) { 
      echo "<a href="{$row->href}">{$row->title}</a>"; 
     } 
} 

假設你的表看起來像

id href   title 
1 http://google.de Go to gooogle! 
2 http://twitter.de Go to twitteeeerr! 
0

好吧,這只是一個例子塞納里奧你給你做什麼的想法。

好讓說,我們有1 textbox形式與名稱url和一個提交按鈕,我們還使用了POST方法。

<form method="post" action="pageurl.php"> 
<input type="text" name="url" value="" /> 
<input type="submit" value="Submit" /> 
</form> 

pageurl.php我們會再有這樣的事情:

<?php 

    //Get the value of $_POST['url'] and cleanse it. 
    $url = htmlentities(stripslashes(mysql_real_escape_string($_POST['url']))); 

    //Echo out the click-able link. 
    echo '<a href="' . $url . '">' . $url . '</a>'; 

?> 

例如,如果我們進入http://twitter.com到我們會得到與文本http://twitter.com這也將鏈接到一個可點擊的鏈接形式twitter.com

0

在處理頁面:

<?php 
include('config.php'); 

$link = $_POST['link']; 
$title = $_POST['title']; 

$sql = "INSERT INTO table_name (link) VALUES ('<a href=\'$link\'>$title</a>')"; 

?> 

並獲取鏈接:

<?php 

include('config.php'); 

//code to select the table 

$sql="SELECT * FROM table_name"; 
$result=mysql_query($sql); 

while($rows=mysql_fetch_array($result)){ 

echo "$rows['link']"; 

} 

?> 
0

我這個糾結了一段時間發現了我最努力的解決方案之前。我會發布它,因爲它可能會幫助別人。我只是玩一個簡單的URL /評論PHP腳本,需要一個URL/Comment並插入到數據庫。然後我顯示結果。起初,網址在顯示時不可點擊。所以我着手尋找一種方法來做到這一點。這裏是我工作的片段:

while($row = mysqli_fetch_array($result)) 
    { 
    echo "<tr>"; 
    echo "<td><a href=" . $row['url'] . ">" . $row['url'] . "</a></td>";//this line is it 
    echo "<td>" . $row['description'] . "</td>"; 
    echo "</tr>"; 
    } 
+0

我應該補充說,到目前爲止我的腳本中沒有異常處理。這隻適用於URL輸入正確的情況。 –

1

這適用於我...

使用DB表列「ID」的「href」,「標題」,如上面的@DarkDevine

<div id="buttons"> 
<ul id="list"> 
     <?php 
     $connect = mysql_connect("host","user","password"); 
      if(!$connect){ 
      die("Failed to connect: " . mysql_error()); 
     } 
      if(!mysql_select_db("YOUR DATABASE NAME")){ 
      die("Failed to select DB:" . mysql_error()); 
     } 
     $results = mysql_query("SELECT * FROM YOU TABLE NAME"); 
      while($row = mysql_fetch_array($results)){  
      echo '<li>' . '<a href="' . $row['href'] . '">' . $row['title'] . '</a>' . '</li>' . '<br/>'; 
     } 
     ?> 
</ul> 

0

提到我有獲取信息出來的後製作環節沒有問題數據庫。我的邏輯是使用<?php echo '$status'; ?>所以如果你說<a href="http://site.com">Click here</a>鏈接將結束回聲功能。雖然如果你做的不同,你可能得不到相同的結果。

0

有一個更簡單的方法來做到這一點。 With a row named「website」

Website:< a href =「http://'.$website。'」target =「_ blank」>'。$ website。'

表格條目是「www.whateverthewebsiteis.com」,輸出將是「http://www.whateverthewebsiteis.com

+0

它不工作? – Vir