【问题标题】:PHP Twitter-like mentions with notifications带有通知的 PHP 类似 Twitter 的提及
【发布时间】:2013-07-23 17:38:35
【问题描述】:

我正在尝试创建一个类似于 Twitter 的系统来提及用户并在您被提及时接收通知。有很多问题在这里提出了类似的问题,但似乎没有一个是决定性的并且以任何方式帮助我。

我的第一个麻烦是在发布状态时解析提及,它在某种程度上有效,但我不擅长需要使用的表达式。如果我发布了一个状态,例如说“@user1 hello there its @user2”,那么 user1 会正确解析,@ 是第一个字符,但我的 user2 将空格显示为解析的配置文件链接的一部分,这是我的 preg_replace 和表达式:

$STRING = preg_replace('/(^|\s)(@\w+)/','<a href="profile.php?u=$0">$0</a>', $STRING);

我还需要知道如何在配置文件链接中只显示不带 @ 的用户名,当前的 $0 也会产生 @ 符号。

至于通知,我有一个名为 alerts 的表,结构如下:

标识 |用户名 |发送者 |留言 |收到了

消息字段包含通知消息内容。我想知道如何从我提交的表单中提取任何提及并检查用户名是否存在,然后从那里为用户创建一个警报,告知他们在帖子中被提及。

【问题讨论】:

    标签: php regex preg-replace


    【解决方案1】:

    至于你的第一个正则表达式,你可以这样做:

    $string = preg_replace('/(^|\s)@(\w+)/', '$1<a href="">$2</a>', $string);
    

    这应该会为您提供不带初始空格或 @ 符号的用户名。基本上,我们将字符串的初始空间或开头捕获到一个变量中,并在替换的开头将其打印出来。你最终在 $2 中得到的是 only @ 符号后面的内容。您也可以通过后视来完成此操作,但我觉得这更简单。

    至于您的数据库结构,也许这样可以:

    messages -
    id | user_id | message
    
    mentions -
    message_id | user_id
    

    对于每次提及,您将 user_id 插入到消息的表中。您可以选择是要实时发送警报还是等待批处理(或者可能 - 根据性能要求 - 只需在需要时通过加入表格来查询与提到的用户的帖子)。

    【讨论】:

    • 感谢正则表达式是完美的,至于通知,我想保持它与我的警报一样,我只需要知道如何在发布表单并插入每个内容时提取提及将它们添加到警报表中,谢谢。
    • 一些笔记给你。在保存评论之前,最好用他们的 id 替换用户名,比如 。当用户更改其用户名时,它可以防止指向用户个人资料的链接无效。然后,您可以简单地提取提及中的用户 ID,并使用此正则表达式 preg_match_all('//', $comment, $users) 将它们替换为他们的用户名。
    【解决方案2】:

    我已经考虑这个问题几个小时了,虽然我有点新手,但我相信我有一种方法可以提取提及和警报并将它们存储到两个一维数组中。

        <?php
             //a variable ($string) that I thought might look like what you are describing
             $string='@steve how are you? @tom nice to hear from you. So happy that you joined @joe, cool! @mike sweeet!';
             //regex to pull out the mentions and the messages 
             preg_match_all('/@(\w+)|\s+([(\w+)\s|.|,|!|?]+)/', $string, $result, PREG_PATTERN_ORDER);
             for ($i = 0; $i < count($result[0]); $i++) {
                $mention[$i]= $result[1][$i];
                $message[$i]= $result[2][$i];
             }
             //test to make sure that all mentions are stored
             for ($j = 0; $j< $i; $j++){
                echo $mention[$j],'<br/>';
             }
             //test to make sure that all messages are stored
             for ($k = 0; $k< $j; $k++){
                echo $message[$k],'<br/>';
             }
        ?>
    

    Regex Buddy 提供的解释,我使用的正则表达式:@(\w+)|\s+([(\w+)\s|.|,|!|?]+):

    Match either the regular expression below (attempting the next alternative only if this one fails) «@(\w+)»
     Match the character “@” literally «@»
      Match the regular expression below and capture its match into backreference number 1 «(\w+)»
      Match a single character that is a “word character” (letters, digits, and underscores) «\w+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» 
     Or match regular expression number 2 below (the entire match attempt fails if this one fails to match) «\s([(\w+)\s|\.|,|!|?]+)»
    Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s+»
     Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
    Match the regular expression below and capture its match into backreference number 2 «([(\w+)\s|\.|,|!|?]+)»
      Match a single character present in the list below «[(\w+)\s|\.|,|!|?]+»
         Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
         The character “(” «(»
         A word character (letters, digits, and underscores) «\w»
         One of the characters “+)” «+)»
         A whitespace character (spaces, tabs, and line breaks) «\s»
         The character “|” «|»
         A . character «\.»
         One of the characters “|,!?” «|,|!|?»
    

    这甚至会返回消息中被括号偏移的单词(例如 (hello))。您应该能够使用数组中定义的变量执行您描述的任何操作。如果这不正确,或者您无法做到,请告诉我,我会看看我能想出什么。

    【讨论】:

      【解决方案3】:

      请查看本教程至implement twitter style mention system with PHP, MYSQL and jQuery

      您已使用正则表达式匹配从内容中提取@提及

      谢谢

      【讨论】:

      • 请在您的回答中总结链接的内容。外部内容可能会下线或发生变化,使您的答案对于将来可能需要帮助的其他人来说已经过时...
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-17
      • 1970-01-01
      • 2016-08-28
      相关资源
      最近更新 更多