简体   繁体   中英

Creating comma-separated text after applying regex on excel column

I have a Excel column and i need to convert it into CSV. I have been given a javascript regex to apply on the column values:

var regex = new RegExp("[^a-z0-9',.]+","gi");
return input.replace(regex, "_").replace(/_+/g, "_").replace(/^_|_$|^\/|\/$|'|,/g, "");

How can I convert excel columns into a javascript array so that I can loop through array values and apply the regex to save it into the array again and then create a csv from the array.

I just have one column in my excel. Any suggestion or best way to solve this? I can use PHP or C# but I am not sure how to convert this javascript regex into php or c#. Or at least some one tell me what should be the best solution to solve this.

This is the sample data from my excel sheet i have one column, str name , and it has values like:

strName
ABC
Nike & Sony etc
THE METEORS

There's no need to convert anything to Javascript as there is a built-in function to create CSV in PHP: fputcsv . It takes an array as input and prints it to an output file as comma-separated data, with a carriage return at the end.

Your data appears to be an array of lines, so you will need to convert each line into an array for use with fputcsv .

# input data
$lines = array(
    'strName',
    'ABC',
    'Nike & Sony etc',
    'THE METEORS');

# open the output file
$fh = fopen('file.csv', 'w');

foreach ($lines as $l) {
    # convert the line to an array and pass it to fputcsv
    fputcsv($fh, array($l));
}
fclose($fh);

file.csv now contains:

strName
ABC
"Nike & Sony etc"
"THE METEORS"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM