Check the word after a '@' character in PHP

前端 未结 4 1678
我在风中等你
我在风中等你 2021-02-20 03:25

I\'m making a news and comment system at the moment, but I\'m stuck at one part for a while now. I want users to be able to refer to other players on the twitter style like

4条回答
  •  独厮守ぢ
    2021-02-20 03:44

    This is where regular expressions come in.

    
    
    1. The / at beginning and end are delimiters (don't worry about these for now).
    2. \w stands for a word character, which includes a-z, A-Z, 0-9, and _.
    3. The (? is a bit advanced, but it's called a negative lookbehind assertion, and means, "An @ that does not follow a word character." This is so you don't include things like email addresses.
    4. The \w+ means, "One or more word characters." The + is known as a quantifier.
    5. The parentheses around \w+ capture the portion parenthesized, and appear in $matches.

    regular-expressions.info seems to be a popular choice of tutorial, but there are plenty of others online.

提交回复
热议问题