2013-07-23 64 views
0

我正在嘗試創建一個類似於Twitter的系統,提及用戶並在提及時收到通知。在這裏有很多問題提出類似的問題,但沒有一個看起來是確鑿的,並以任何方式幫助我。類似Twitter的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也會產生@符號。

至於通知,我有一個表叫這種結構警報:

ID |用戶ID | sentby |消息|收到

消息字段包含通知消息內容。我想知道如何從提交的表單中提取任何提及,並檢查用戶名是否存在,然後從那裏爲用戶創建一條警告,提醒他們已在帖子中提及。

回答

3

關於你的第一個正則表達式,你可以做這樣的事情:

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

這應該讓您的用戶名,而不初始空間或@符號。基本上,我們將字符串的初始空間或開頭捕獲到一個變量中,並在替換開始時打印出來。以$ 2結尾的只有 @符號後面的內容。你也可以用逆序來完成,但我覺得這很簡單。

至於你的數據庫結構,或許會有這樣的事:

messages - 
id | user_id | message 

mentions - 
message_id | user_id 

對於每個提到,您將USER_ID到表的消息。您可以選擇是否希望實時發送警報或等待批處理過程(或者可能 - 根據性能要求 - 只需通過加入表格即可查詢提及用戶的帖子)。

+0

由於正則表達式是完美的,作爲通知,我想保留它我的提醒方式,我只需要知道如何提取提及時,我張貼表單,並將他們每個插入警報表,謝謝。 – Engine

+0

給你一些筆記。在保存評論之前,用戶可以用他們的ID替換用戶名,例如。它防止在用戶更改其用戶名時無法連接到用戶配置文件。 然後,你可以簡單地提取用戶id在提及,並用這個正則表達式preg_match_all('/ /',$ comment,$ users)將它們替換爲他們的用戶名。 – honzahommer

1

我一直在想這個問題幾個小時,雖然我有點新手,但我相信我有一種方法可以提取提及和警報並將它們存儲到兩個一維數組中。

<?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/>'; 
     } 
    ?> 

的解釋,如正則表達式好友提供的),我使用了正則表達式:@(\ 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 「|,!?」 «|,|!|?» 

這甚至會返回由圓括號(例如(你好))偏移的消息中的單詞。您應該能夠使用數組中定義的變量來執行您描述的任何操作。如果這是不正確的,或者你不能,讓我知道,我會看到我能想出什麼。