0

here is my code to view posts from users, but it show link as normal text.how to detect url and convert into clickable link? reference attached:

enter image description here

function get_posts()
{
    global $con;
    $get_posts = "SELECT * FROM posts";
    $run_posts = mysqli_query($con, $get_posts);
    while ($row_posts = mysqli_fetch_array($run_posts)) {
        $post_id = $row_posts['post_id'];
        $user_id = $row_posts['user_id'];

        $content   = $row_posts['post_content'];
        $post_date = $row_posts['post_date'];

        echo "<div class='posts'>
        <p>$post_date</p>
        <p>$content</p>
        <a href='single.php?post_id=$post_id'Style='float:right;'>
        <button>See Replies or Reply to is</button></a>
        </div><br/>";
    }
}
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Kailash
  • 5
  • 4
  • I don't really understand what are you trying to do, please read [ask] and edit your question to make it clear for the people ^^ – alex55132 May 15 '19 at 13:27
  • your code supposed to work, I've tested it and it shows the button with the link – Sethu May 15 '19 at 13:29
  • I mean, when user post, it show everything as text, links also as text. check reference: https://imgur.com/lDSWjf4 – Kailash May 15 '19 at 13:35

2 Answers2

1

Check below code as you have not properly prepared html. You have used button inside the anchor tag.

function get_posts(){
    global $con;
    $get_posts="SELECT * FROM posts";
    $run_posts=mysqli_query($con,$get_posts);
    while($row_posts=mysqli_fetch_array($run_posts)){
        $post_id=$row_posts['post_id']; 
        $user_id=$row_posts['user_id']; 

        $content=$row_posts['post_content'];    
        $post_date=$row_posts['post_date']; 

        echo '<div class="posts">
            <p>'.$post_date.'</p>
            <p>'.$content.'</p>
            <a href="single.php?post_id='.$post_id.'" style="float:right;">
        See Replies or Reply to is</a>     
        </div><br/>';
    }
}
Jaimin Panchal
  • 289
  • 1
  • 9
0

This solution will catch all http/https/www and convert to clickable links.

$url = '~(?:(https?)://([^\s<]+)|(www\.[^\s<]+?\.[^\s<]+))(?<![\.,:])~i'; 
$content = preg_replace($url, '<a href="$0" target="_blank" title="$0">$0</a>', $content);

Please see Convert plain text URLs into HTML hyperlinks in PHP.

ASSILI Taher
  • 1,210
  • 2
  • 9
  • 11