查找表达式中的所有 words/string
Find all words/string in an expression
假设我有这样的表达
$string = ( score + total-score - total-min_score) / papoy
我希望能够将所有 words/term 提取到数组中(单词 with/without 破折号和下划线)
我试过了(我对正则表达式不是很好)
preg_match("(\w+-_)",$string,$matches);
但它只是 return 我第一场比赛。我怎样才能得到所有匹配项?
您需要使用preg_match_all功能。
preg_match_all('~[\w-]+~',$string,$matches);
或
preg_match_all('~\w+(?:-\w+)*~', $string, $matches);
假设我有这样的表达
$string = ( score + total-score - total-min_score) / papoy
我希望能够将所有 words/term 提取到数组中(单词 with/without 破折号和下划线)
我试过了(我对正则表达式不是很好)
preg_match("(\w+-_)",$string,$matches);
但它只是 return 我第一场比赛。我怎样才能得到所有匹配项?
您需要使用preg_match_all功能。
preg_match_all('~[\w-]+~',$string,$matches);
或
preg_match_all('~\w+(?:-\w+)*~', $string, $matches);