如何从数据库显示中获取链接作为可点击的活动链接?

时间:2011-10-11 08:53:55

标签: php mysql html

我有一个提交和显示文字的表单。例如,输入http://www.twitter.com以明文形式显示。如何让它自动显示为可点击链接?

我正在使用MySQL和PHP。

9 个答案:

答案 0 :(得分:3)

如果数据是URL,您只需将其包装在标签中:

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));

答案 1 :(得分:2)

你可能希望你的脚本能够自动检测链接......几周前我在一个从RSS Feed中提取推文的脚本上做了这个......这就是代码。您可以用适当的变量替换$ _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>';
}

希望有所帮助

答案 2 :(得分:1)

这对我有用......

使用带有“id”“href”“title”列的数据库表,如上所述@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>

答案 3 :(得分: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!

答案 4 :(得分:0)

好的,这只是一个例子,你可以让你知道该怎么做。

好的,我们假设有一个名为textbox的{​​{1}}表单和一个提交按钮,我们也使用了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,该链接也会链接到twitter.com

答案 5 :(得分: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']";

}

?>

答案 6 :(得分:0)

在找到最适合我的解决方案之前,我对此进行了一段时间的努力。我会发布它,因为它可能会帮助其他人。我只是在玩一个简单的URL / Comment PHP脚本,它接受一个URL / Comment并插入到DB中。然后我显示结果。首先,显示时无法点击URL。所以我开始寻找一种方法来做到这一点。这是适用于我的代码片段:

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>";
   }

答案 7 :(得分:0)

从数据库中获取信息后,我没有问题。我的逻辑是使用<?php echo '$status'; ?>所以如果你说<a href="http://site.com">Click here</a>链接将在echo函数内部结束。虽然如果你做的不同,你可能得不到相同的结果。

答案 8 :(得分:0)

有一种更简单的方法可以做到这一点。 有一行名为“网站”

网站:&lt; a href =“http://'.$website。”“target =”_ blank“&gt;'。$ website。'

表单条目为“www.whateverthewebsiteis.com”,输出为“http://www.whateverthewebsiteis.com