这是我的代码,用于查看用户的帖子,但将链接显示为普通文本。如何检测网址并将其转换为可点击的链接?随附参考:
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/>";
}
}
答案 0 :(得分:1)
请检查以下代码,因为您没有正确准备html。您已经在定位标记中使用了按钮。
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/>';
}
}
答案 1 :(得分:0)
此解决方案将捕获所有http / https / www并转换为可点击的链接。
$url = '~(?:(https?)://([^\s<]+)|(www\.[^\s<]+?\.[^\s<]+))(?<![\.,:])~i';
$content = preg_replace($url, '<a href="$0" target="_blank" title="$0">$0</a>', $content);