search
HomeBackend DevelopmentPHP Tutorial评论或文章里添加@用户名,被@用户可以收到通知的功能怎么实现?

评论或文章里添加@用户名,该用户名自动加上一个用户主页地址的标签,同时被@用户可以收到通知,请问这个功能怎么实现呢?

回复内容:

每一条评论和文章发完之后 会对@后面的内容匹配分组,比如到一个分隔符一类的规则,然后异步压入一个消息队列。

这个队列处理是否有存在用户,对存在的用户做消息广播。

后端逻辑大概这样子吧。编辑一类不会有处理,只在insert这一步做。

这里可以抽象出来一个查找at用户的接口,可以抽象出一个检测at后面是否用户合法的接口。

前端我也做过,web的话,直接对富文本里的change做变化,发现一个at之后,操作range选择最近的一个到at或者分隔符的字符再去请求这个字符key列出当前用户的好友或者全站的用户like的提示,展现上就是个浮层加光标位置的定位计算。鼠标选择后 可能会对文本打标记。

这一步主要是增加后端匹配速度,发现标记就不用再对文本做正则了。可以把标记的通知用户在前端就解析出来发给后端了。

这里可能每个人实现逻辑都不一样。大概的步骤应该就是以上。 来来来,我们一起研究一下知乎是怎么做的。
首先明白一点,在答案中at某人的时候,显示的是昵称,而昵称在知乎里是可以重复的,那么,很明显,是不能直接根据这个昵称来判断at的是哪个人的。
我们可以试着在答案里@某个人 ,首先我们能看到,@后面的用户名部分字体颜色变了,和前后的文字样式是不一样的,说明at部分应该是用某个标签包围起来的,F12打开浏览器调试功能,定位到答案中@部分的html代码,如下图:
评论或文章里添加@用户名,被@用户可以收到通知的功能怎么实现?
可见,知乎用一个a标签把@和用户名包围了起来,注意红色圈起来的data-hash的属性和class=“member_mention”,class属性可以改变样式也可以表示这里at了某个人,而data-hash属性很可能就是用于识别被at用户的唯一标识符,而这样答案提交之后,知乎服务器就可以根据这个member_mention和hash这两个关键属性准确快速识别出您at的用户了(当然,我不是知乎员工, 这只是个人猜测,不一定是知乎的真正实现方式,但是这种方式是可行的),然后就可以给相应的用户发送通知了,识别出用户之后,至于显示用户名片的功能,只是给简单的js和css问题而已。 百度贴吧就是直接@某人 然后解析的。
知乎这种是输入@ 之后选一个人,是确定的,不需要再去解析。直接把这个人的id提交给后台。
可能这个跟百度贴吧id唯一,知乎昵称不唯一也有一定关系。 在应用层专门加一个解析逻辑,从用户提交的内容里抓出at就行,考虑到知乎的编辑器还可以实时显示,说明后台有个api把这个功能暴露出来了。 解析出@后面的第一个合法用户名,然后想怎么发通知都可以。。。
也可以像知乎这样,随着你打字实时提示让你选取 正则匹配后看用户名存在否
Discuz有这方面的源码 看情况。
如果内容不可再编辑,怎么实现都行。
如果可以,那么你的业务需求提得并不足够明确。
Statement
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools