Get line number from preg_match_all()

前端 未结 10 1075
天命终不由人
天命终不由人 2021-02-07 09:23

I\'m using PHP\'s preg_match_all() to search a string imported using file_get_contents(). The regex returns matches but I would like to know at which line number those matches a

10条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-07 09:55

    //Keep it simple, stupid
    
    $allcodeline = explode(PHP_EOL, $content);
    
    foreach ( $allcodeline as $line => $val ) :
        if ( preg_match("#SOMEREGEX#i",$val,$res) ) {
            echo $res[0] . '!' . $line . "\n";
        }
    endforeach;
    

提交回复
热议问题