2

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

2
  • 2
    Which IDE are you using? Different IDEs could lead to different answers about how to approach this problem. Commented Feb 21, 2019 at 22:26
  • I am using PhpStorm Commented Feb 22, 2019 at 8:32

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I have never used that. Thanks for the pointers. When I try to search with what you suggested, I get all the function listings, but how do I export all the patterns? [![Please see this screenshot][1]][1] [1]: i.sstatic.net/1yChz.png The export option exports all search results, I just want the patterns to be exported.
Hm hm, not sure if there is a way to be that precise with export, sorry. :( I guess this doesn't satisfy extraction part of your needs then.
I think you best go here is do use Find in Path to find all preg_* functions, then place a caret on a pattern, invoke context menu > refactor > extract > variable. PhpStorm would automatically replace it with a var of your preference. You can then refactor vars into constants later on. The downside is you would have to do that manually. But I guess it's Ok since you would still have to create constants for each of patterns.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.