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

Not needed

Not needed
0
HuaMinChen
Asked:
HuaMinChen
  • 7
  • 5
3 Solutions
 
Ray PaseurCommented:
Not answered
0
 
Julian HansenCommented:
Hmm, clouded the question is.
1
 
HuaMinChenBusiness AnalystAuthor Commented:
Hi all,
Sorry to that I've removed the previous posted details as I thought I could resolve it.

By these
   while (!feof($fh)) 
	  {
     		  $line = fgets($fh);
                  $linea = str_replace('"','',$line);
                  $lineb = str_replace(',','',$linea);
                  $linec = str_replace("\t", "|", $lineb);
                  //echo "$linec";
                  
		  $seg = preg_split("/[|]/",$linec);
                 
			
			if ($error == 0)
		     {
			
				
					$dfile=0;
                                        
					$usql = "select 'x' from tb_bm_report_edi_bkgforecast where rtrim(filename)='".$file."'";
                                        //echo "$usql";
					$dfileuk = odbc_exec($connection,$usql);
					//echo "$dfileuk";
					$dfile = odbc_result($dfileuk, 1);
                                        //echo "start";
					//echo "$dfile";
                                        //echo "end"; 
					if ($dfile == "x")
                                                {$error=2;}

					if (strlen($linec) !=0)
                                          {
                                        
					
					if (count($seg) != 25 )
						{$error =1;}
                                        if (strlen($seg[0]) !=3)
        	                                echo "$linec"."9";
					if (strlen($seg[1]) !=7)
        	                                echo "$linec"."10";
					if (strlen($seg[2]) != 10)
        	                                echo "$linec"."11";
					if (strlen($seg[3]) > 13)
        	                                echo "$linec"."12";
					if (strlen($seg[4]) > 3)
        	                                echo "$linec"."13";
					if (strlen($seg[5]) > 5)
        	                                echo "$linec"."14";
					if (strlen($seg[6]) > 5)
        	                                echo "$linec"."15";
					if (strlen($seg[7]) >3)
        	                                echo "$linec"."16";
					if (strlen($seg[8]) > 10)
        	                                echo "$linec"."17";
					if (strlen($seg[9]) > 10)
        	                                echo "$linec"."18";
					if (strlen($seg[10]) > 10)
        	                                echo "$linec"."19";
					if (strlen($seg[11]) > 10)
        	                                echo "$linec"."20";
					if (strlen($seg[12]) > 10)
        	                                echo "$linec"."21";
					if (strlen($seg[13]) > 10)
        	                                echo "$linec"."22";
					if (strlen($seg[14]) > 10)
        	                                echo "$linec"."23";
					if (strlen($seg[15]) > 10)
        	                                echo "$linec"."24";
					if (strlen($seg[16]) > 10)
        	                                echo "$linec"."25";
					if (strlen($seg[17]) > 10)
        	                                echo "$linec"."26";
					if (strlen($seg[18]) > 10)
        	                                echo "$linec"."27";
					if (strlen($seg[19]) > 10)
        	                                echo "$linec"."28";
					if (strlen($seg[20]) > 10)
        	                                echo "$linec"."29";
					if (strlen($seg[21]) > 10)
        	                                echo "$linec"."30";
					if (strlen($seg[22]) > 10)
        	                                echo "$linec"."31";
					if (strlen($file) > 100)
        	                                echo "$linec"."32";
					if ($error == 1)	
 					{echo "$linec";
					echo "startyy17gg";
					echo "$error";
					echo "end";
					break;}
		                           }

Open in new window

it is scanning relevant Text file line by line. But I cannot identify where the problem even if I've tried to show echo line in above. Any good way to identify/locate the problem?

Here is the relevant Text file for processing.
https://app.box.com/s/scqvowczjfydyc1s3ys3s9b0qvs8dpmm
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Julian HansenCommented:
I am not sure what you are trying to do with the data but this might help
This code uses fgetcsv to break the lines into fields. The code dumps the first 10 records. You will see from the dump that the resulting array contains the different field values which can be accessed by their index.
<?php
$filename = 't1869.txt';
$fh = fopen($filename, 'rt');
$bom = fread($fh, 2);
if (bin2hex($bom) != 'fffe') {
	fseek($fh, 0);
}
$count = 0;
while($line = fgetcsv($fh, 1000, "\t", '"')) {
	if ($count++ <10) {
		fnDump($line);
	}
}

function fnDump($obj)
{
	echo "<pre>" . print_r($obj, true) . "</pre>";
}

Open in new window

Note there is Byte Order Mark (FFFE) at the start of the file. You should either save your file as UTF8 before processing or use the code above that checks for the BOM and ignores it if found
0
 
HuaMinChenBusiness AnalystAuthor Commented:
Sorry Julian.
I run this PHP file

https://app.box.com/s/y02i1is6bbzyz2ou97w6gxw4tui7z77e

to process the Text file but I can not identify the problem within Text file.

I call this batch

C:\AppServ\php5\php.exe D:\edi\Zim_A4BF_EDI\ZBAM_SCAN_INSERT.php
exit

Open in new window

to call PHP file.
0
 
Julian HansenCommented:
Ok but that file is very nuts and bolts and does things in a very inefficient way.

Replacing all the \t characters with a | so you can do a preg_split - when fgetcsv will read the line and convert it to an array for you - you could achieve the same result in a third of the lines that are much easier to debug.
0
 
HuaMinChenBusiness AnalystAuthor Commented:
Very sorry to that, I really NEED to know why the given PHP file is stuck there, due to the file. Any other advice?

I want to know why such Text file finally was saved into Error folder, after having been processed. Why?
0
 
Julian HansenCommented:
As I mentioned the code is complex with database dependencies to debug it is going to take time - rewriting it using the above mentioned method may be quicker.

Having said that you have this in your code
			if (count($seg) != 25 )	{
				$error =1;
			}

Open in new window

And as there is a blank line at the end of the file you gave us I am guessing that the error is being set because of that blank line.

You probably want to add this to your loop
while (!feof($fh)) {
	$line = trim(fgets($fh));
	if (empty($line)) continue;

Open in new window

0
 
Julian HansenCommented:
There is also a byte order mark at the start of the file (FF FE) - that your code does not seem to take into account - not sure if that is causing a problem but it will add 2 extra bytes to the first field in the first record
0
 
HuaMinChenBusiness AnalystAuthor Commented:
There is also a byte order mark at the start of the file (FF FE) - that your code does not seem to take into account - not sure if that is causing a problem but it will add 2 extra bytes to the first field in the first record

Yes, it now does stop on 1st line. How to deal with such byte order mark? By what kind of tool, you get known there is byte order mark, to the file?
0
 
Julian HansenCommented:
Look at lines 4-8 of my sample above
$bom = fread($fh, 2);
if (bin2hex($bom) != 'fffe') {
	fseek($fh, 0);
}

Open in new window

Code reads the first 2 bytes - if BOM not detected it resets the file pointer.
0
 
HuaMinChenBusiness AnalystAuthor Commented:
I put your codes into current PHP file like

   while (!feof($fh)) 
	  {
		$bom = fread($fh, 2);
		if (bin2hex($bom) != 'fffe') {
			echo "seeking";
			fseek($fh, 0);
		}
     		  $line = fgets($fh);
                  $linea = str_replace('"','',$line);
                  $lineb = str_replace(',','',$linea);
                  $linec = str_replace("\t", "|", $lineb);
                  //echo "$linec";

Open in new window

                 
but I still encounter the same problem.
0
 
Julian HansenCommented:
But do you still have a blank line at the end of the data - are you checking for the blank line and ignoring the record if it is blank. See this post
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now