/    Sign up×
Community /Pin to ProfileBookmark

Line Numbers with preg_match

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

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@temp_user123Jul 06.2007 — I haven't tried it but, a thought... If you capture, using [B]preg_match_all()[/B], 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.
×

Success!

Help @patenaudemat spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...