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 are found. What’s the best technique to achieve this?
I could read the file as an array and perform the regex for each line, but the problem is that my regex matches results across carriage returns (new lines).
well it’s kinda late, maybe you alrady solved this, but i had to do it and it’s fairly simple.
using
PREG_OFFSET_CAPTUREflag inpreg_matchwill return the character position of the match.lets assume $charpos, so
voilá!