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>";
 }
4
  • Can you post your code about echoing the url? Commented Feb 22, 2014 at 16:51
  • It is just the standard way using mysqli mysqli_fetch_array Commented Feb 22, 2014 at 16:53
  • If you post the code we might help you Commented Feb 22, 2014 at 16:54
  • Is that okay @Fabio ? Commented Feb 22, 2014 at 17:01

4 Answers 4

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/

Sign up to request clarification or add additional context in comments.

4 Comments

I know how to create the link! I just need a way for the php to recognise that part of it is a link
Filter it using a regular expression. Take a look at this link: link
Exactly what I wanted!
Copy and paste it into your answer?
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:

2 Comments

+1 because useful info. But not really answering the question. So not sure if I am abiding the law here
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 :)
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

Comments

0
Use this
echo '<a href="' . $res['url'] . '">' . $res['url'] . '</a>';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.