Solved

Not needed

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

Expert Comment

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

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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 55

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 55

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
 
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 55

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 55

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 55

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 55

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Gift cards are not a new concept - it's been around for a very long time.  Undoubtedly, over the past you have received such a card or purchased one for a friend or relative.  Are you aware that you've been feeding the machine?  If not, read on :)
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

830 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