1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Array cleaning, quotes and decimals

Discussion in 'PHP' started by TemporaryFailure, Jun 14, 2017.

  1. #1
    I have script which makes csv file from xls-file. But I need to get rid of quotes and decimals and also replace delimiter , to ;

    Original:
    5.67710K,"1,00"
    5.72160K,"-1,00"
    5.75800K,"2,00"
    5.81240K,"0,00"

    cleaned:
    5.67710K;1
    5.72160K;-1
    5.75800K;2
    5.81240K;0

    How to do it?
    Code which cleans empty rows below. What to add there?
    
    if (false !== ($ih = fopen($input, 'r'))) {
      $oh = fopen($output, 'w');
    
      while (false !== ($data = fgetcsv($ih, 80000, ","))) {
      if ($data[2] != NULL and $data[6] != NULL) {   
      $outputData = array($data[2], $data[6]);
      fputcsv($oh, $outputData);
     }
      }
    
      fclose($ih);
      fclose($oh);
    }
    
    PHP:
     
    TemporaryFailure, Jun 14, 2017 IP
  2. hdewantara

    hdewantara Well-Known Member

    Messages:
    536
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #2
    I think you could use trim() for the double-quotes,
    then just concatenate strings with implode() ?
    And since the resulting string is no longer a valid CSV line,
    fputcsv() will not be the right one.
     
    hdewantara, Jun 14, 2017 IP
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #3
    @hdewantara has it right that fputcsv is the wrong function, because what you seem to want for a result IS NOT CSV! Just fwrite it with the semi-colon and \r\n at the end.

    
    if ($ih = fopen($input, 'r')) {
    	$oh = fopen($output, 'w');
    	while ($data = fgetcsv($ih, 65528)) {
    		if (
    			array_key_exists(2, $data) &&
    			array_key_exists(6, $data) &&
    			!empty($data[2]) &&
    			!empty($data[6])
    		) fwrite($oh, $data[2] . ';' . (int) $data[6] . "\r\n");
    	}
    	fclose($ih);
    	fclose($oh);
    }
    
    Code (markup):
    -- edit -- oh, and don't go so utterly batshit on the literal boolean comparisons. Waste of code... comma is the default delimiter, and I would REALLY worry if you have single lines in your CSV that break the 64k minus 64 bit pointer size mark... don't check for null as the access will be invalid and still thrown an error (this isn't JavaScript) that's why "array_key_exists" and "empty" exists, and you should be using logical "&&" not the higher precedence "and"
     
    Last edited: Jun 14, 2017
    deathshadow, Jun 14, 2017 IP
  4. TemporaryFailure

    TemporaryFailure Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #4
    Thank you. Works great. I'm a newbie with these. My solutions are mainly taken from google search results :D
     
    TemporaryFailure, Jun 14, 2017 IP