0

So I have three pages one that is the index page. One that writes the data from a form inside the index page to the database. And one that gets data from the database and echos out a html table with the data inside.

Currently if you write a link in the form. It will just come out as text. I would like the whole link to be like <a href="[link]">[link]</a>.

so say if I wrote this onto the form:

Look at this: www.google.com or Look at this: https://www.google.com

it would come out like this in html

Look at this: <a href="www.google.com">www.google.com</a>

How could I go about doing this?


Okay so the html is:

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

in which the user would write:

"Look at this: www.google.com or Look at this: https://www.google.com"

This would then get sent to the database through Write-to.php.

$sql="INSERT INTO social (comunicate)

VALUES

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

this then gets written back into the database:

$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>";
 }
maxisme
  • 3,974
  • 9
  • 47
  • 97

4 Answers4

1

Just try:

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

Update:

The OP really wanted to detect url's in a string. One possible solution could be filter it using a regular expression. This code could help:

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

}
?>

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

MillaresRoo
  • 3,808
  • 1
  • 31
  • 37
1

There are quite a few things you need to worry about when displaying user supplied (tainted) data.

function validateUrl($url) {
    return filter_var($url, FILTER_VALIDATE_URL); 
}
  • What you are attempting to do is convert a string into a link, for example you can write a function like this:
function makeClickable($link) {
    $link = htmlspecialchars($link);
    return sprintf('<a href="%s">%s</a>', $link, $link);
}
  • You can use string concatenation as well, but I wouldn't do that in my view code. Just personal preference.

  • Take a look at the urlencode function, it will certainly come in handy.

  • I would also recommend you read about cross site scripting

Please note that I am not making any implementation recommendations, my aim is just to show you some contrived code samples that demonstrate making a string "clickable".

Update: If you would like to make links clickable within text, refer to the following questions:

Community
  • 1
  • 1
ymas
  • 474
  • 6
  • 10
  • +1 because useful info. But not really answering the question. So not sure if I am abiding the law here – maxisme Feb 22 '14 at 17:26
  • My "makeClickable" function will make a valid user supplied link clickable, there are a few cases you will need to cater for, for example are you sure you're always going to get valid urls? Sometimes users don't bother with http:// etc. The best I can do is give you a contrived simple demo and you can adapt it to your use case :) – ymas Feb 22 '14 at 17:35
0

save the hyperlink in db and retrieve as a string by sql query like:

select link from table_name where index = i and save link as:<a href="whatever"> whaatever here</a>

and print it

0
Use this
echo '<a href="' . $res['url'] . '">' . $res['url'] . '</a>';
rakeshjain
  • 1,791
  • 11
  • 11