如何在php中检索文本作为可点击链接?

这是我查看用户帖子的代码,但它将链接显示为普通文本。如何检测 url 并转换为可点击的链接?附上参考:

http://img1.mukewang.com/60cda3ee0001fe9704080414.jpg

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

    }

}


DIEA
浏览 172回答 2
2回答

泛舟湖上清波郎朗

此解决方案将捕获所有 http/https/www 并转换为可点击链接。$url&nbsp;=&nbsp;'~(?:(https?)://([^\s<]+)|(www\.[^\s<]+?\.[^\s<]+))(?<![\.,:])~i';&nbsp; $content&nbsp;=&nbsp;preg_replace($url,&nbsp;'<a&nbsp;href="$0"&nbsp;target="_blank"&nbsp;title="$0">$0</a>',&nbsp;$content);
打开App,查看更多内容
随时随地看视频慕课网APP