0

I'm trying to check for

  1. '@username'
  2. '@username,'
  3. '@username '
  4. '@username.'
  5. '@username. '
  6. '@username:'
  7. '@username: '

I currently have this:

$post->message = preg_replace(
    '/@ *('.preg_quote($speak['username'], '/').') *:/i',
    '[url=\''.PAGE_URL.RELATIVE_WBB_DIR.'/index.php?page=User&userID='.$speak['toID'].'\']@'.$speak['username'].':[/url]',
    $post->message);

Does anyone have any ideas how I can modify this to accept those inputs?

4

2 回答 2

0

修改您的正则表达式以添加标点符号:

$post->message = preg_replace(
    '/@ *('.preg_quote($speak['username'], '/').')[:,.]? */i',
    '[url=\''.PAGE_URL.RELATIVE_WBB_DIR.'/index.php?page=User&userID='.$speak['toID'].'\']@'.$speak['username'].':[/url]',
    $post->message);

注意添加的部分:

/@ *('.preg_quote($speak['username'], '/').')[:,.]? */i
                                             ^^^^^^

可以选择匹配其中一个字符(冒号、逗号、句点)。

于 2013-01-22T15:10:15.647 回答
0

这是一个应该实现您正在寻找的正则表达式:

/^@[a-zA-Z]+[:\.\,]?[ ]?$/

/^@         // string starts with '@'
[a-zA-Z]+   // contains any letter at least once
[:\.\,]?    // may contain any : , . zero or one time
[ ]?$       // may contain a whitespace zero or one time at end of string
于 2013-01-22T15:34:45.650 回答