9 Digit and Row Count via Regex Notepad ++

5

1st Doubt:

How to convert a phonebook that is in the format:

1188888888
1288888888
3188888888
1187877766

To:

11988888888
12988888888
31988888888
11987877766

via notepad ++ regular expression?

Or maybe if possible in PHP, as I have to loop through multiple files. If it is possible to do this batch in notepad and someone can explain me xD

Remembering that DDD can change. The whole list is of cell phones, so you do not need confirmation to know if it is or is not. I just needed the regex to walk the first 2 houses and add 9.

I tried to do some phishing in PHP Dom but it did not work, much less using fopen ... I was able to print the nice result, but I can not export the final value to the csv back. If so, I'll do this via notepad ++ myself. It is more laborious, but it seems to be a simpler solution.

If anyone knows of any application or easiest way to do this ...

2nd Doubt After adding the 9 digit, I need to add a semicolon and the line count, going something like this:

11988888888;1
12988888888;2
31988888888;3
11987877766;4

Of course, the list is huge, so it would need to be in a very conventional way. If you can do both things at the same time with just the application of 1 regex, it would be even cooler: D

PS: Now that I've noticed that the last line of all files is blank. That is, if there are 5000 rows in the file, 4999 has numbers, the last one is not. Then the count should be broken only where it has number, not to "break" the format of the file.

Could anyone do that? Thanks: D

MY PHP SOLUTION (not functional because it does not save the result, it only displays on the screen)

<?php
$row = 1;
$handle = fopen ("Lista (1).csv","r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $num = count ($data);
    for ($c=0; $c < $num; $c++) {
    $datanew = substr_replace($data, "9", 2, 0);
        echo $datanew[$c]. ";$row <br />\n";
        $row++;
    }
}
fclose ($handle);
?>  

But it only prints on the screen. I could not get it to overwrite the file.

    
asked by anonymous 13.10.2015 / 20:56

1 answer

5

Notepad ++ / Column mode

Notepad ++ can solve this problem in three steps.

1 - Add 9, place the strokes in the third character of the first line and activate the column menu edit> column mode or ( ALT + C ), dialog box type 9 and ok.

2 - Repeat step 1, but this time leave the cursor at the end of the line, instead of 9 add ; .

3 - Access the column mode again, leave the cursor after ; , in the dialog box mark number to insert in initial number put 1 which will be the number of the first line and leave increse by in 1 , finally ok.

PHP

Withphpdoesnotchangemuchlogicasthestringlookswellformatted,thiscanbedonebasedonsize.

substr_replace()isresponsibleforfirstaddingthe9tothe2positionofthemanipulatedline.Anotheradditionoccursinthecharacter12(endofstring)where;isadded,followedbythelinenumber($linha_atual)edeumquebradelinha(PHP_EOL),tudoissoéconcatenadoem$new_filequeéoconteúdodonoarquivo,eleécriadocomfile_puts_contents()'.

<?php$csvs=glob('*.csv');foreach($csvsas$index=>$nome){$linha_atual=1;$arquivo_novo="";

        $handle = fopen ($nome, 'r');
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $linha_modificada = substr_replace($data[0], '9', 2, 0);
            $arquivo_novo .= substr_replace($linha_modificada, ';'. $linha_atual 
                             .PHP_EOL , '12');    
            $linha_atual++;
        }
        fclose ($handle);
        file_put_contents('novo'. $index .'.csv', $arquivo_novo);
    }
    
13.10.2015 / 21:05