如果您只想要行号,请使用:
$msg = 'Opening and ending tag mismatch: en line 44 and goods';
if (preg_match('#\bline (\d+)#', $msg, $matches)) {
echo "line is: " . $matches[0] . "\n";
}
Run Code Online (Sandbox Code Playgroud)
如果您想一次匹配所有行号:
$msgs = <<<EOF
If you want to match all lines in all messages at once:
Opening and ending tag mismatch: en line 44 and goods
Opening and ending tag mismatch: describtion line 40 and categorie
Opening and ending tag mismatch: categorieInfo line 28 and card
Premature end of data in tag categorie line 27
Premature end of data in tag card line 2
EOF;
preg_match_all('#^.*\bline (\d+).*$#m', $msgs, $matches, PREG_SET_ORDER);
foreach($matches as $msg) {
echo "message: " . $msg[0] . "\n";
echo "line: " . $msg[1] . "\n";
}
Run Code Online (Sandbox Code Playgroud)