Php 仅从字符串的第一行获取

Php 仅从字符串的第一行获取,php,regex,php-5.3,Php,Regex,Php 5.3,我有这个字符串: error_id: 44 error_de: wrong number : 565 现在我想得到error\u id的每一个值。我该怎么做呢?你需要使用preg\u match\u all,因为有不止一个(如果我理解正确的话?) <?php if (preg_match('/error_id: (\d+)/', $string, $matches)) { print_r($matches); } 例如,单击preg\u match\u all 编辑:如果您

我有这个字符串:

error_id: 44
error_de: wrong number : 565

现在我想得到
error\u id
的每一个值。我该怎么做呢?

你需要使用preg\u match\u all,因为有不止一个(如果我理解正确的话?)

<?php

if (preg_match('/error_id: (\d+)/', $string, $matches)) {
    print_r($matches);
}
例如,单击preg\u match\u all


编辑:如果您也需要“错误号码”部分,请使用此模式:
错误id:\s+(\d+).error\de:(.*):(\d+)
您需要使用preg\u match\u all,因为有多个(如果我理解正确的话?)

例如,单击preg\u match\u all


编辑:如果您也需要“错误号码”部分,请使用此模式:
error\u-id:\s+(\d+).error\u-de:(.*)(\d+)

preg\u-match('~error\u-id:\s*\K\d+~,$string)
也包括
error\u-de
预匹配('~error\d:\s*\K\d+~,$string)
也包括
error\u-de
<?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_all("/error_id:\s+(\d+)/", $theString, $errorIDs);
Var_dump($errorIDs[1]);