?
Solved

Not needed

Posted on 2016-11-15
13
Medium Priority
?
148 Views
Last Modified: 2016-11-17
Not needed
0
Comment
Question by:HuaMinChen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
13 Comments
 
LVL 111

Expert Comment

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

Expert Comment

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

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
Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

 
LVL 58

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 11

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 58

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 11

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 58

Assisted Solution

by:Julian Hansen
Julian Hansen earned 2000 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 58

Assisted Solution

by:Julian Hansen
Julian Hansen earned 2000 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 11

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 58

Accepted Solution

by:
Julian Hansen earned 2000 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 11

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 58

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

770 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