Shaunak Sontakke
Shaunak Sontakke

Reputation: 1270

Find all patterns used in preg_* functions in PHP code

I have around 4k PHP files. There are more than 6k preg_* functions used in the code.

I need to find all patterns used in the code and extract them. My aim is to group such patterns and replace them with a named constant.

I want to make a list like this: https://www.exakat.io/reports/codeigniter/datas/inventories_regex.html

I tried to make a regex search, but I'm not getting it right.

regex: https://regexr.com/48s1s

Upvotes: 2

Views: 68

Answers (2)

Rarst
Rarst

Reputation: 2395

The best tool I can think of for such task is structural search in PhpStorm (commercial software).

The main advantage is that it treats this kind of search as code, so it is much more discerning than regex or other search pattern that can only process text.

So you can do something like $function$($pattern$,$argument$,$anotherargument$), set filter on function to match multiple functions, and set search to capture pattern part.

Upvotes: 2

Hefekranz
Hefekranz

Reputation: 1

Depending on the variations of those patterns, I would recommend using "Find and Replace" of an IDE, for example with Atom or Visual Studio Code

  • find a preg_* function in your codebase
  • extract the used pattern into a constant
  • find the pattern in your files
  • replace it with the defined constant

It might be quite some manual work, but you'll be able to do it step by step

Upvotes: 0

Related Questions