查找表达式中的所有单词/字符串


Find all words/string in an expression

假设我有类似的表达式

$string = ( score + total-score - total-min_score) / papoy

我希望能够将所有单词/术语提取到一个数组中(带/不带短划线和下划线的单词)

我试过像(我不太擅长正则表达式)

preg_match("('w+-_)",$string,$matches);

但这只会让我回到第一场比赛。我怎么能拿到所有的火柴?

您需要使用preg_match_all函数。

preg_match_all('~['w-]+~',$string,$matches);

preg_match_all('~'w+(?:-'w+)*~', $string, $matches);

演示