function getNumbersBeforeChar($string, $char) { $pattern = '/(d+)(?=' . preg_quote($char) . ')/'; preg_match($pattern, $string, $matches); return isset($matches[1]) ? $matches[1] : ''; } // 示例使用 $string = '123abc'; $char = 'a'; $result = getNumbersBeforeChar($string, $char); echo $result; // 输出: 123
查看原内容