Solved

Not needed

Posted on 2016-11-15
13
58 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 51

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 51

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 51

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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 51

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 51

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 51

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 51

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Articles on a wide range of technology and professional topics are available on Experts Exchange. These resources are written by members, for members, and can be written about any topic you feel passionate about. Learn how to best write an article t…

743 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

14 Experts available now in Live!

Get 1:1 Help Now