Solved

Not needed

Posted on 2016-11-15
13
91 Views
Last Modified: 2016-11-17
Not needed
0
Comment
Question by:HuaMinChen
  • 7
  • 5
13 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41889513
Not answered
0
 
LVL 52

Expert Comment

by:Julian Hansen
ID: 41889548
Hmm, clouded the question is.
1
 
LVL 10

Author Comment

by:HuaMinChen
ID: 41890782
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
 
LVL 52

Expert Comment

by:Julian Hansen
ID: 41890895
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
 
LVL 10

Author Comment

by:HuaMinChen
ID: 41890908
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
 
LVL 52

Expert Comment

by:Julian Hansen
ID: 41890955
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 10

Author Comment

by:HuaMinChen
ID: 41890963
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
 
LVL 52

Assisted Solution

by:Julian Hansen
Julian Hansen earned 500 total points
ID: 41891001
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
 
LVL 52

Assisted Solution

by:Julian Hansen
Julian Hansen earned 500 total points
ID: 41891004
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
 
LVL 10

Author Comment

by:HuaMinChen
ID: 41891019
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
 
LVL 52

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 41891048
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
 
LVL 10

Author Comment

by:HuaMinChen
ID: 41891076
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
 
LVL 52

Expert Comment

by:Julian Hansen
ID: 41891078
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This story has been written with permission from the scammed victim, a valued client of mine – identity protected by request.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Saved searches can save you time by quickly referencing commonly searched terms on any topic. Whether you are looking for questions you can answer or hoping to learn about a specific issue, a saved search can help you get the most out of your time o…

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now