如何提取标签内容?

ann*_*nna 5 php regex

我试图使用PHP提取标签后面的内容.

例如:

$sentence = 'This #Coffee is the #best!!';
Run Code Online (Sandbox Code Playgroud)

我如何获得' 咖啡 '和' 最佳 '的价值?注意,我不想要' 最好 ' 之后的感叹号

Wri*_*ken 15

在utf-8/unicode中非常安全:

preg_match_all('/#([\p{L}\p{Mn}]+)/u',$string,$matches);
var_dump($matches);
Run Code Online (Sandbox Code Playgroud)

虽然,如果你没有使用/期待异国情调的角色,这可能同样有效并且更具可读性:

preg_match_all('/#(\w+)/',$string,$matches);
Run Code Online (Sandbox Code Playgroud)