2016-05-23 43 views
0

我有這個字符串:只得到第一行

error_id: 44 
error_de: wrong number : 565 

現在我想要得到的error_id值的每次出現。我怎樣才能做到這一點?

+0

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

+0

包括'erro r_de'以及? – chris85

回答

1
<?php 

if (preg_match('/error_id: (\d+)/', $string, $matches)) { 
    print_r($matches); 
} 

我想你會想利用$matches[1]但我不記得手!

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

1

您需要使用的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+)