我想用php中的字符串中提取以@
开头的所有单词。
哪种方式最好?
编辑:我不想收到电子邮件由于
答案 0 :(得分:8)
$matches = null;
preg_match_all('/(?!\b)(@\w+\b)/','This is the @string to @test.',$matches)
使用preg_match_all并利用前瞻开头的单词((?!\b)
)和单词分隔符(\b
),您可以轻松实现这一目标。细分:
/ # beginning of pattern
(?!\b) # negative look-ahead for the start of a word
( # begin capturing
@ # look for the @ symbol
\w+ # match word characters (a-z, A-Z, 0-9 & _)
\b # match until end of the word
) # end capturing
/ # end of pattern
<强> demo 强>