• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 358
  • Last Modified:

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

0
rgb192
Asked:
rgb192
  • 3
  • 2
1 Solution
 
Ray PaseurCommented:
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

0
 
rgb192Author Commented:
i would like all the just numbers and letters in the output
0
 
Ray PaseurCommented:
Try this REGEX to get numbers and letters
function clean_string($str)
{
    // LETTERS NUMBERS ONLY
    $regex = "/[^A-Za-z0-9]/";
    $str = trim($str);
    $str = preg_replace($regex, '', $str);
    return $str;
}

Open in new window

0
 
rgb192Author Commented:
thanks
0
 
Ray PaseurCommented:
Thanks for the points, ~Ray
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now