You are here:  » Get rid of space or other special character inside each field


Get rid of space or other special character inside each field

Submitted by mwong on Fri, 2006-08-11 21:12 in

First of all, the parser is GREAT,
I am just asking if I can do some add-on function to it:

here is the situation:

I get something like

[FIELD1] = "99999 "

how can I get rid of space and "(double quote) ?

thanks,

Derek

Submitted by mwong on Fri, 2006-08-11 21:14

here is the code I use:

$survey_ans= array();
require("MagicParser.php");

function survey_ans_Handler($record){

global $survey_ans;
$survey_ans[]=$record;
}
MagicParser_parse("SRVYANS.txt","survey_ans_Handler","csv|124|0|0");

Submitted by support on Sat, 2006-08-12 06:30

Hi Derek;

Firstly, it looks like the feed you are using has quoted text fields, so you want to use the Format String "csv|124|0|34" (which means text format, no header row, quoted text).

Secondly, as you are loading the entire file into an array, handling the white space will need to be done when you use the fields themselves. Do remove the space, use PHP's trim() function. For example:

<?php
  $survey_ans
= array();
  require(
"MagicParser.php");
  function 
survey_ans_Handler($record){
    global 
$survey_ans;
    
$survey_ans[]=$record;
  }
  
MagicParser_parse("SRVYANS.txt","survey_ans_Handler","csv|124|0|34"
  
foreach($survey_ans as $ans)
  {
    print 
trim($ans["FIELD1"]);
  }
?>

That will print each FIELD1, without spaces. I presume that you were intending to use a FOREACH statement anyway to process each record after reading everything into a global array.

Hope this helps!
David.