从字符串中获取第一行

时间:2016-05-23 14:22:10

标签: php regex php-5.3

我有这个字符串:

error_id: 44
error_de: wrong number : 565

现在我希望每次出现error_id的值。我怎么能这样做?

3 个答案:

答案 0 :(得分:1)

<?php

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

我想你会想要使用$matches[1],但我不记得了!

答案 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

答案 2 :(得分: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+)

相关问题