Regular Expressions (RegEx) → How to take a text list and surround each line in quotes

The Problem

If you have a list from a text file such as this:

Item1
Item2
Item3
Item4
Item5

And you want this, let’s say for an array:

array(
'Item1',
'Item2',
'Item3',
'Item4',
'Item5',
);

The Solution

You need to use a find and replace in your IDE (I’m using PHPStorm). Enable the ability to RegEx in your Find & Replace.

Then for your find:

(.+)

And your replace:

\’$1\’,

This will select all the word for you and add the quotes and the comma. The $1 is a RegEx variable that has a value of the results it found. $1 means the first result from your RegEx search.

Hope this helps

Happy Coding!

Show your support

Clapping shows how much you appreciated Tony Mucci’s story.