将标记用户名链接到配置文件

时间:2018-10-14 20:01:07

标签: php laravel function preg-match

此刻,我将帖子正文中@后面的每个单词都链接到个人资料。以下代码在post模型中。问题是,如果配置文件不存在,它也会被链接。

如何确保个人资料存在,然后才能链接到个人资料?

如果用户的隐私为3,则个人资料应链接到ID,而不是用户名。但是Auth :: user()可以看到他的用户名。

<?php

protected function replaceUserNames($body)
{
    preg_match_all('/@(\w+)/', $body, $users);
    if (!isSet($users[1])) {
        return $body;
    }
    $userList = Collect($users[1]);
    foreach ($userList->unique() as $user) {
        $body = str_replace('@' . $user, 
            '<a href="/profile/' . $user . '">@' . $user . '</a>', $body);
    }

    return $body;
}

0 个答案:

没有答案
相关问题