Click to See Complete Forum and Search --> : Line Numbers with preg_match


patenaudemat
07-06-2007, 11:14 AM
Hey there,

I'm doing some source code processing with preg_match, and ideally, I'd like to search for a pattern, and then identify what line number the match occurred on with each match.

What is the best way to do this? Should I split my code by \n, then attempt the match on each line, or is there an efficient way to do it with PREG_OFFSET_CAPTURE? Or something else even?

Thanks in advance!

-Matt

temp.user123
07-06-2007, 11:22 AM
I haven't tried it but, a thought... If you capture, using preg_match_all(), both what you're searching for and occurrences of newline (using the or | operator in the regular expression), possibly the line number would reveal itself by the order in which the saved matches were stored in the resulting match array.