Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

csv square character

the csv file sometimes get a square character

how to clean up input/ output
<?php 
 
require_once('../common.php');     
$sql = "SELECT * FROM productfolders WHERE active = '1' ORDER BY name";  
$products = dbfetcharray($sql);  
$fp = fopen('file.csv', 'w');  
$cols = array('folderid', 'name'); 
fputcsv($fp, $cols);  
foreach($products as $p){ 
fputcsv($fp, array($p['folderid'], $p['name']) ); 
  
}  
fclose($fp);  
 
?>

Open in new window

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

What character set do you want in the output?

You can clean up the data with a REGEX maybe something like this...
foreach($products as $p)
{ 
   $f = clean_string($p['folderid']);
   $n = clean_string($p['name']);
   fputcsv($fp, array($f, $n) ); 
}  

function clean_string($str, $len=255)
{
    // BLANK LETTERS NUMBERS UNDERSCORE DOT HYPHEN APOSTROPHE AT-SIGN ONLY
    $regex = "/[^ A-Za-z0-9_@\.\-\']/";
    $str = trim($str);
    $str = preg_replace($regex, '', $str);
    $str = substr($str,0,$len);
    return $str;
}

Open in new window

Avatar of rgb192

ASKER

i would like all the just numbers and letters in the output
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

ASKER

thanks
Thanks for the points, ~Ray