2

I have a form that submits and displays text. Entering http://www.twitter.com for example displays in clear text. How can i get it to automatically display as a clickable link?

I am working with MySQL and PHP.

halfer
  • 19,824
  • 17
  • 99
  • 186
algorithmicCoder
  • 6,595
  • 20
  • 68
  • 117

9 Answers9

3

If the data is a URL, you can just wrap it in a tag:

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

Things get trickier if you need to convert the URL in the text. Most users won't bother with escaping the links correctly, and post example.com/Some Thing - [Me] instead of http://example.com/Some%20Thing%20-%20%5BMe%5D. Find a regular expression that fit your needs and use:

echo preg_replace('#expression that matches a URL#', '<a href="$1">$1</a>',
    htmlspecialchars($your_data, ENT_QUOTES));
Community
  • 1
  • 1
Lekensteyn
  • 64,486
  • 22
  • 159
  • 192
2

You propably want your script to automatically detect links...I did that a few weeks ago on a script that pulls tweets from an RSS Feed...this is the code. You can replace $_POST['inputfield'] with the appropriate variable!

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

Hope that helps

Anonymous
  • 3,679
  • 6
  • 29
  • 40
  • Upside with my solution is btw that you can use strpos and the $link_end_pos and $link_start_pos to also output the text after/before the actual link :) – Anonymous Oct 11 '11 at 09:28
1

This works for me...

using a DB table with columns "id" "href" "title", as mentioned above by @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

On the processing page:

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

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

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

?>

And to retrieve the link:

<?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']";

}

?>
Greg
  • 26
  • 4
0

I wrestled with this for some time before finding the solution that best worked for me. I'll post it as it might help someon else. I am just playing around with a simple URL/Comment PHP script that takes a URL/Comment and inserts to the DB. I then display the results. At first the URLs weren't clickable when displaying. So I set out to find a way to do that. Here's the snippet that worked for me:

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>";
   }
Kevin_A
  • 1
  • 1
  • I should add that there is no exception handling in my script so far. This only works if the URL is entered correctly. – Kevin_A Aug 17 '13 at 16:31
0

I have no problem with Making link after getting information out of a database. my logic is using <?php echo '$status'; ?> So if you say <a href="http://site.com">Click here</a> The link will end up inside the echo function. Although if you do it different you might not get the same result.

Brian
  • 1
0

There is a far easier way to do this. With a row named "website"

Website: < a href="http://'.$website.'" target="_blank">'.$website.'

the form entry is "www.whateverthewebsiteis.com" and the output would be "http://www.whateverthewebsiteis.com"

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

assuming your table looks like

 id href            title
 1  http://google.de    Go to gooogle!
 2  http://twitter.de   Go to twitteeeerr!
DarkDevine
  • 1,047
  • 1
  • 9
  • 12
0

Okay this is just an example senario for you to give you an idea of what to do.

Okay lets say we have a form with 1 textbox with the name url and a submit button, we are also using the POST method.

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

In pageurl.php we would then have something like this:

<?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>';

?>

For example if we entered http://twitter.com into the form we would get a click-able link with the text http://twitter.com which would also link to twitter.com

RobFos
  • 951
  • 6
  • 24