只得到第一行

问题描述:

我有这个字符串:只得到第一行

error_id: 44 
error_de: wrong number : 565 

现在我想要得到的error_id值的每次出现。我怎样才能做到这一点?

+0

'preg_match('〜error_id:\ s * \ K \ d +〜',$ string)' –

+0

包括'erro r_de'以及? – chris85

<?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+)