如果您希望匹配每一行,请使用preg_match_all() + 使用m 修饰符:
$string = 'sometext moretext 01 text
text sometext moretext 002
text text 1 (somemoretext)
etc';
preg_match_all('~^(.*?)(\d+)~m', $string, $matches);
您所有的结果都在$matches
数组中,如下所示:
Array
(
[0] => Array
(
[0] => sometext moretext 01
[1] => text sometext moretext 002
[2] => text text 1
)
[1] => Array
(
[0] => sometext moretext
[1] => text sometext moretext
[2] => text text
)
[2] => Array
(
[0] => 01
[1] => 002
[2] => 1
)
)
输出示例:
foreach ($matches[1] as $k => $text) {
$int = $matches[2][$k];
echo "$text => $int\n";
}