1

I have a file called "file.php" contained lines like this (not only) :

 ...
 define("META_PAGE_BRAND_HOME_TITLE","esta es la Marca");
 define("META_PAGE_BRAND_HOME_DESCRIPTION","Conoce nuestra Marca y empieza con la web ");
 define("META_PAGE_BRAND_HOME_KEYWORDS","Marca Logo Mision");
 ...

I would like to get this:

{
 "meta_page_brand_home_title":"esta es la Marca",
 "meta_page_brand_home_description":"Conoce nuestra Marca y empieza con la web ",
 "meta_page_brand_home_keywords":"Marca Logo Mision"
 }

I would like to rewrite those only the lines beginning with "define(" into the console or a new file , caps in the first part should be lowercase . I know I should do something like this but I'm not that sharp to achieve so. Any help would be appreciated.

Community
  • 1
  • 1
MikZuit
  • 684
  • 5
  • 17

2 Answers2

1

You should be able to achieve what you are trying to do with an advanced text editor like Sublime Text.

With Sublime you will be able to do multiple select on

define(

using a method method here: https://www.sublimetext.com/docs/3/multiple_selection_with_the_keyboard.html

Once you've got that you can remove all those lines.

You can do the same with

);

And replace it with the

,

as requried.

Further, Sublime will allow you to change the case of selected words to all lower case, as you are required.

Newteq Developer
  • 2,257
  • 1
  • 26
  • 32
1

The first step that you'd have to do is to actually read the contents of your file, which can be done using fs.readFile().

fs.readFile('path/to/file.php', function(err, fileContents) {
    if (err) {
        throw err;
    }
    // `fileContents` will then contain the contents of your file.
});

Once you have the contents of your file, you will then need to find the define() calls using regex and use it inside the code before:

var regex = /define\((".*?"), *(".*?")\)/g;
var match = regex.exec(fileContents);
// `fileContents` contains the contents of your file.

while(match) {
    // match[1] will contain the first parameter to the "define" call
    // match[2] will contain the second parameter to the "define" call
    // use match[1] and match[2] however you want, like log it to the console:
    console.log(match[1].toLowerCase() + ':' + match[2] + ',');

    // Look for the next match
    match = regex.exec(fileContents);
}
Arnelle Balane
  • 5,437
  • 1
  • 26
  • 32