只得到第一行
答
<?php
if (preg_match('/error_id: (\d+)/', $string, $matches)) {
print_r($matches);
}
我想你会想利用$matches[1]
但我不记得手!
答
<?php
// Your string.
$string = 'error_id: 44 error_de: wrong number : 565';
// Find the error id from your string ($string).
if(preg_match('/error_id\:\s+([\d]+)/', $string, $matches))
{
// Echo out the error id number
echo $matches[1];
}
有关preg_match的更多信息,请参阅http://php.net/manual/en/function.preg-match.php。
答
您需要使用的preg_match _all因为有不止一个
preg_match_all("/error_id:\s+(\d+)/", $theString, $errorIDs);
Var_dump($errorIDs[1]);
工作为例,点击preg_match_all
http://www.phpliveregex.com/p/fMH
编辑(如果我理解正确吗?):如果你需要“错误的数字”部分也使用此模式:error_id:\s+(\d+).error_de: (.*) : (\d+)
'preg_match('〜error_id:\ s * \ K \ d +〜',$ string)' –
包括'erro r_de'以及? – chris85