Home > Backend Development > PHP Tutorial > 类似新浪的@匹配用户是怎么做的啊

类似新浪的@匹配用户是怎么做的啊

WBOY
Release: 2016-06-06 20:52:12
Original
1017 people have browsed it

如题.... 前端做法没什么压力,就是后端怎么对匹配用户进行匹配加url啊~~

回复内容:

如题.... 前端做法没什么压力,就是后端怎么对匹配用户进行匹配加url啊~~

微博的做法其实是这样的,id,uid,昵称都是唯一的。
http://weiboc.om/{id}
http://weibo.com/u/{uid}
http://weibo.com/n/{screen_name}

三个字段都建立唯一索引。@后也没必要处理URL,展现时前台用JS匹配昵称,点击链接后通过路由决定查找的字段,然后跳转到对应主页。

@的文字是用户名,而链接是id,所以需要一个用户名到id的查询,是缓存,nosql还是普通的db看你的业务。
然后php可以写一个preg_replace_callback
举例:

$text = preg_replace_callback('/@(\w+)/u', uname, $text);

function uname($matches) {
    $uname = $matches [1];
    $uid = get_uid_by_uname($uname);
    return "<a href="%5C%22/u/%7B%24uid%7D%5C%22">{$uname}</a>";
}
Copy after login

自动完成

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template