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

LVL 1
rgb192Asked:
Who is Participating?
 
Ray PaseurConnect With a Mentor Commented:
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
 
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
 
rgb192Author Commented:
thanks
0
 
Ray PaseurCommented:
Thanks for the points, ~Ray
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.