zardiw Posted January 7, 2008 Share Posted January 7, 2008 Ok. Totally at a loss here. Reading a text file from a server. I've exploded a comma delimited line of text into an array. Print_r shows $ExpParts[7] to be a capital 'I'. However, this statement evaluates as false: If $ExpParts[7] == 'I' . strcmp($ExpParts[7], 'I') gives me a value of 2. Any help here would be appreciated. I've looked and googled until there's only a few small tufts of hair left on my head: print_r output: Array ( [0] => $ACD [1] => 01/04/2008 [2] => 4070.19 [3] => 4070.19 [4] => 4006.70 [5] => 4008.95 [6] => 0 [7] => I ) Code: $ExpParts = explode(",", $ExpFilLine); print_r ($ExpParts); If (($ExpParts[7] == 'D') or ($ExpParts[7] == 'U')) $ValidInp = True; $Eq = strcmp($ExpParts[7], 'I'); print ('Equal is -' . $Eq); If ($ExpParts[7] == 'I') print ('Its an I for crissakes'); If ($Eq == 0) print ('Its a F.....g I'); z Quote Link to comment Share on other sites More sharing options...
priti Posted January 7, 2008 Share Posted January 7, 2008 hi, kindly post $ExpFilLine line so that one can track your problem. regards Quote Link to comment Share on other sites More sharing options...
zardiw Posted January 7, 2008 Author Share Posted January 7, 2008 $ACD,12/14/2007,4147.43,4147.43,4130.07,4135.01,0,I Btw, looking at in in Hex, that I at the end is a 49 ................z Quote Link to comment Share on other sites More sharing options...
priti Posted January 7, 2008 Share Posted January 7, 2008 hi, I wrote this code to check $str='$ACD,12/14/2007,4147.43,4147.43,4130.07,4135.01,0,I'; $str_arr=explode(',',$str); echo '<pre>'; print_r($str_arr); if(trim($str_arr[7]) == 'I') { echo 'printing I'; } else { echo 'whats this'; } now it is printing me I whats the problem???/ regards Quote Link to comment Share on other sites More sharing options...
zardiw Posted January 7, 2008 Author Share Posted January 7, 2008 The problem is you're not reading a flat .txt file off a server. You're comparing apples to apples...........obviously I'm comparing apples to oranges somehow..........z Quote Link to comment Share on other sites More sharing options...
zardiw Posted January 7, 2008 Author Share Posted January 7, 2008 Here's the file: http://www.ddamanda.com/stkall20080104.txt ...........z Quote Link to comment Share on other sites More sharing options...
priti Posted January 7, 2008 Share Posted January 7, 2008 The problem is you're not reading a flat .txt file off a server. You're comparing apples to apples...........obviously I'm comparing apples to oranges somehow..........z Even if you are going to read a flat .txt how it matters??? you might be doing something $str=file_get_contents('file'); this function return the file content in string . and if not like this then you sud tell us how you are doing and in your code what this signifyies 'applies==orange'???? or 'apple == apple' If ($ExpParts[7] == 'I') print ('Its an I for crissakes'); get cool and tell me which function you are using for reading file in string? Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted January 7, 2008 Share Posted January 7, 2008 hi, kindly post $ExpFilLine line so that one can track your problem. regards As priti said, can you post your whole code and not just that snippet? Please. Quote Link to comment Share on other sites More sharing options...
priti Posted January 7, 2008 Share Posted January 7, 2008 Try this $file_arr_content=file('dummy.txt'); use file command to read file in array REPLACE THIS DUMMY.TXT WITH YOUR TXT FILE I TOOK SOME LINES FRM YOUR FILE AND PROCESSED IT. ---------------- Reading file in array------------ print_r($file_arr_content); ------------ Now explode usign ','-------------- $str_arr=explode(',',$file_arr_content[0]); if(trim($str_arr[7]) == 'I') { echo 'printing I'; } else { echo 'whats this'; } Quote Link to comment Share on other sites More sharing options...
zardiw Posted January 7, 2008 Author Share Posted January 7, 2008 $filename = "stkall" . $CurrUpdDate . ".txt"; print ($filename); $count = 0; $fd = fopen ($filename, "r") or exit("Unable to open file!"); $ExpFilLine = fgets($fd); $ExpParts = explode(",", $ExpFilLine); nextDat(); It originally had a .sto extension, and I uploaded it using the html form 'file' upload function. I'm thinking it uploaded it as binary instead of ASCII or something..........still playing around with it, but these types of problems really freak me out............there's enough programming type bugs/problems without something this simple throwing a monkey wrench into the works.............z Quote Link to comment Share on other sites More sharing options...
priti Posted January 7, 2008 Share Posted January 7, 2008 try above code posted and let us know still you are in same problem? Quote Link to comment Share on other sites More sharing options...
zardiw Posted January 7, 2008 Author Share Posted January 7, 2008 Output: stkall20080104.txt$ACD,01/04/2008,4070.19,4070.19,4006.70,4008.95,0,I Array ( [0] => $ACD [1] => 01/04/2008 [2] => 4070.19 [3] => 4070.19 [4] => 4006.70 [5] => 4008.95 [6] => 0 [7] => I ) Equal is -1$ADR,01/04/2008,1034.80,1034.85,1009.66,1010.31,0,I Array ( [0] => $ADR [1] => 01/04/2008 [2] => 1034.80 [3] => 1034.85 [4] => 1009.66 [5] => 1010.31 [6] => 0 [7] => I ) Equal is -1$AJT,01/04/2008,18.12,18.12,17.95,17.95,0,I Array ( [0] => $AJT [1] => 01/04/2008 [2] => 18.12 [3] => 18.12 [4] => 17.95 [5] => 17.95 [6] => 0 [7] => I ) Equal is -1$AMZ,01/04/2008,303.99,303.99,300.95,301.46,0,I Array ( [0] => $AMZ [1] => 01/04/2008 [2] => 303.99 [3] => 303.99 [4] => 300.95 [5] => 301.46 [6] => 0 [7] => I ) Equal is -1 function nextDat() { global $fd; global $ExpFilLine; global $ExpParts; global $count; while(!feof($fd)) { $ValidInp = False; $ExpParts = explode(",", $ExpFilLine); print ($ExpFilLine); print_r ($ExpParts); If (($ExpParts[7] == 'D') or ($ExpParts[7] == 'U')) $ValidInp = True; $Eq = strcmp($ExpParts[7], 'I'); print ('Equal is -' . $Eq); If ($ExpParts[7] == 'I') print ('Its an I for crissakes'); If ($Eq == 0) print ('Its a F.....g I'); $count++; If ($count > 3) die(); If (!$ValidInp) { $ExpFilLine = fgets($fd); continue; } $ExpFilLine = fgets($fd); break; } } Quote Link to comment Share on other sites More sharing options...
zardiw Posted January 7, 2008 Author Share Posted January 7, 2008 try above code posted and let us know still you are in same problem? Ok. Thanks for all the help guys!!.......I'll try that and see what happens..........z Quote Link to comment Share on other sites More sharing options...
priti Posted January 7, 2008 Share Posted January 7, 2008 try above code posted and let us know still you are in same problem? Ok. Thanks for all the help guys!!.......I'll try that and see what happens..........z Hey wait just do this change in your code. $Eq = strcmp(trim($ExpParts[7]), 'I'); and you would be able to solve this .I think extra spaces are the problem so where ever you are comparing just use trim() function . remember : strcmp:Returns < 0 if str1 is less than str2 ; > 0 if str1 is greater than str2 , and 0 if they are equal. Quote Link to comment Share on other sites More sharing options...
zardiw Posted January 7, 2008 Author Share Posted January 7, 2008 Try this $file_arr_content=file('dummy.txt'); use file command to read file in array REPLACE THIS DUMMY.TXT WITH YOUR TXT FILE I TOOK SOME LINES FRM YOUR FILE AND PROCESSED IT. ---------------- Reading file in array------------ print_r($file_arr_content); ------------ Now explode usign ','-------------- $str_arr=explode(',',$file_arr_content[0]); if(trim($str_arr[7]) == 'I') { echo 'printing I'; } else { echo 'whats this'; } $filename = "stkall" . $CurrUpdDate . ".txt"; print ($filename); $count = 0; $file_arr_content=file($filename); // ---------------- Reading file in array------------ print_r($file_arr_content); // ------------ Now explode usign ','-------------- $str_arr=explode(',',$file_arr_content[0]); if(trim($str_arr[7]) == 'I') { echo 'printing I'; } else { echo 'whats this'; } die(); Output: stkall20080104.txtArray ( [0] => $ACD,01/04/2008,4070.19,4070.19,4006.70,4008.95,0,I [1] => $ADR,01/04/2008,1034.80,1034.85,1009.66,1010.31,0,I [2] => $AJT,01/04/2008,18.12,18.12,17.95,17.95,0,I [3] => $AMZ,01/04/2008,303.99,303.99,300.95,301.46,0,I [4] => $BIS,01/04/2008,59.53,59.64,58.80,58.89,0,I [5] => $BIX,01/04/2008,262.05,262.05,250.49,251.45,,I [6] => $BJD,01/04/2008,18.53,18.53,18.27,18.29,0,I [7] => $BKX,01/04/2008,86.10,86.12,83.03,83.25,,I [8] => $BOJ,01/04/2008,31.73,31.73,30.87,30.89,0,I [9] => $BPI,01/04/2008,121.00,121.08,119.86,119.89,0,I [10] => $BTD,01/04/2008,25.98,26.06,25.72,25.79,0,I [11] => $BTK,01/04/2008,779.35,781.78,771.57,773.61,0,I [12] => $BUX,01/04/2008,0.46,0.46,0.43,0.43,0,I [13] => $BVG,01/04/2008,63.69,63.92,61.86,61.97,0,I [14] => $BVH,01/04/2008,64.87,64.89,63.27,63.29,0,I [15] => $BVO,01/04/2008,74.24,74.38,72.13,72.20,0,I [16] => $BVP,01/04/2008,66.31,66.31,64.17,64.25,0,I [17] => $BVS,01/04/2008,51.76,51.76,50.31,50.31,0,I [18] => $BWH,01/04/2008,27.64,27.64,26.29,26.33,0,I [19] => $BWW,01/04/2008,18.59,18.59,18.03,18.04,0,I [20] => $BXD,01/04/2008,193.33,193.33,190.59,190.74,,I [21] => $BXM,01/04/2008,843.25,843.25,829.57,829.83,,I [22] => $BXN,01/04/2008,352.64,352.87,343.61,343.69,,I [23] => $BXR,01/04/2008,166.24,166.24,162.05,162.29,,I [24] => $BXV,01/04/2008,41.89,41.89,40.70,40.71,0,I [25] => $BXY,01/04/2008,980.19,980.19,962.72,963.02,,I [26] => $BYT,01/04/2008,59.35,59.35,55.57,55.96,,I [27] => $CEX,01/04/2008,320.60,320.60,313.52,314.37,,I [28] => $CIB,01/04/2008,487.39,487.81,483.41,485.02,0,I [29] => $CJK,01/04/2008,19.60,19.60,18.85,18.92,0,I [30] => $CKG,01/04/2008,268.41,268.41,259.67,259.87,0,I [31] => $CMR,01/04/2008,722.86,722.86,712.57,712.75,0,I [32] => $COMP,01/04/2008,4310.18,4310.18,4228.40,4230.85,3600269,I [33] => $CRB,01/04/2008,368.61,368.96,364.70,366.22,0,I [34] => $CRX,01/04/2008,894.23,894.23,865.44,866.50,0,I [35] => $CTN,01/04/2008,247.14,247.14,235.99,236.37,0,I [36] => $CVK,01/04/2008,273.87,273.87,264.41,264.84,0,I [37] => $CYC,01/0 There was more....................lol............z Quote Link to comment Share on other sites More sharing options...
zardiw Posted January 7, 2008 Author Share Posted January 7, 2008 Ooops......Here's the end of the output: ZUPC,01/04/2008,0.0230,0.0270,0.0190,0.0190,4016,D [13392] => ZVUE,01/04/2008,1.60,1.60,1.51,1.57,1370,Q [13393] => ZVUEW,01/04/2008,0.57,0.57,0.50,0.50,4,Q [13394] => ZYNX,01/04/2008,1.3000,1.3000,1.2200,1.2600,158,D [13395] => ZYTC,01/04/2008,1.0100,1.0100,1.0100,1.0100,75,U [13396] => ZZ,01/04/2008,10.38,10.51,10.00,10.07,14969,N ) printing I z Quote Link to comment Share on other sites More sharing options...
zardiw Posted January 7, 2008 Author Share Posted January 7, 2008 So the answer is to use this: trim($str_arr[7]) == 'I' Weird.....but THANK YOU!!!................z Quote Link to comment Share on other sites More sharing options...
priti Posted January 7, 2008 Share Posted January 7, 2008 Try this $file_arr_content=file('dummy.txt'); use file command to read file in array REPLACE THIS DUMMY.TXT WITH YOUR TXT FILE I TOOK SOME LINES FRM YOUR FILE AND PROCESSED IT. ---------------- Reading file in array------------ print_r($file_arr_content); ------------ Now explode usign ','-------------- $str_arr=explode(',',$file_arr_content[0]); if(trim($str_arr[7]) == 'I') { echo 'printing I'; } else { echo 'whats this'; } $filename = "stkall" . $CurrUpdDate . ".txt"; print ($filename); $count = 0; $file_arr_content=file($filename); // ---------------- Reading file in array------------ print_r($file_arr_content); // ------------ Now explode usign ','-------------- $str_arr=explode(',',$file_arr_content[0]); if(trim($str_arr[7]) == 'I') { echo 'printing I'; } else { echo 'whats this'; } die(); Output: stkall20080104.txtArray ( [0] => $ACD,01/04/2008,4070.19,4070.19,4006.70,4008.95,0,I [1] => $ADR,01/04/2008,1034.80,1034.85,1009.66,1010.31,0,I [2] => $AJT,01/04/2008,18.12,18.12,17.95,17.95,0,I [3] => $AMZ,01/04/2008,303.99,303.99,300.95,301.46,0,I [4] => $BIS,01/04/2008,59.53,59.64,58.80,58.89,0,I [5] => $BIX,01/04/2008,262.05,262.05,250.49,251.45,,I [6] => $BJD,01/04/2008,18.53,18.53,18.27,18.29,0,I [7] => $BKX,01/04/2008,86.10,86.12,83.03,83.25,,I [8] => $BOJ,01/04/2008,31.73,31.73,30.87,30.89,0,I [9] => $BPI,01/04/2008,121.00,121.08,119.86,119.89,0,I [10] => $BTD,01/04/2008,25.98,26.06,25.72,25.79,0,I [11] => $BTK,01/04/2008,779.35,781.78,771.57,773.61,0,I [12] => $BUX,01/04/2008,0.46,0.46,0.43,0.43,0,I [13] => $BVG,01/04/2008,63.69,63.92,61.86,61.97,0,I [14] => $BVH,01/04/2008,64.87,64.89,63.27,63.29,0,I [15] => $BVO,01/04/2008,74.24,74.38,72.13,72.20,0,I [16] => $BVP,01/04/2008,66.31,66.31,64.17,64.25,0,I [17] => $BVS,01/04/2008,51.76,51.76,50.31,50.31,0,I [18] => $BWH,01/04/2008,27.64,27.64,26.29,26.33,0,I [19] => $BWW,01/04/2008,18.59,18.59,18.03,18.04,0,I [20] => $BXD,01/04/2008,193.33,193.33,190.59,190.74,,I [21] => $BXM,01/04/2008,843.25,843.25,829.57,829.83,,I [22] => $BXN,01/04/2008,352.64,352.87,343.61,343.69,,I [23] => $BXR,01/04/2008,166.24,166.24,162.05,162.29,,I [24] => $BXV,01/04/2008,41.89,41.89,40.70,40.71,0,I [25] => $BXY,01/04/2008,980.19,980.19,962.72,963.02,,I [26] => $BYT,01/04/2008,59.35,59.35,55.57,55.96,,I [27] => $CEX,01/04/2008,320.60,320.60,313.52,314.37,,I [28] => $CIB,01/04/2008,487.39,487.81,483.41,485.02,0,I [29] => $CJK,01/04/2008,19.60,19.60,18.85,18.92,0,I [30] => $CKG,01/04/2008,268.41,268.41,259.67,259.87,0,I [31] => $CMR,01/04/2008,722.86,722.86,712.57,712.75,0,I [32] => $COMP,01/04/2008,4310.18,4310.18,4228.40,4230.85,3600269,I [33] => $CRB,01/04/2008,368.61,368.96,364.70,366.22,0,I [34] => $CRX,01/04/2008,894.23,894.23,865.44,866.50,0,I [35] => $CTN,01/04/2008,247.14,247.14,235.99,236.37,0,I [36] => $CVK,01/04/2008,273.87,273.87,264.41,264.84,0,I [37] => $CYC,01/0 There was more....................lol............z Because your text file is very big ( Well you should know this before ) .I think you need to give some time to your browser to perform the whole set of action and then check the output.If it fails then script will time out. Quote Link to comment Share on other sites More sharing options...
zardiw Posted January 7, 2008 Author Share Posted January 7, 2008 For Google/Forum Search: character comparison problem string compare problem Use trim to solve character or string comparison problem solution comparing strings in flat text file ..........z Quote Link to comment Share on other sites More sharing options...
zardiw Posted January 7, 2008 Author Share Posted January 7, 2008 Yeah, I should have taken a portion of it........no problem, I've got PHP set for extra time........Thanks again. But why didn't it work, i.e. why do you need to do the Trim function (not sure what that does, but I guess it trims extraneous crap off or something)..........z Quote Link to comment Share on other sites More sharing options...
priti Posted January 7, 2008 Share Posted January 7, 2008 Yeah, I should have taken a portion of it........no problem, I've got PHP set for extra time........Thanks again. But why didn't it work, i.e. why do you need to do the Trim function (not sure what that does, but I guess it trims extraneous crap off or something)..........z well ' I' is not equal to 'I'.... because space is considered while you compare. so when you do comparision you have to trim off all exta spaces from left and right side of your string. Quote Link to comment Share on other sites More sharing options...
zardiw Posted January 7, 2008 Author Share Posted January 7, 2008 Yeah, I should have taken a portion of it........no problem, I've got PHP set for extra time........Thanks again. But why didn't it work, i.e. why do you need to do the Trim function (not sure what that does, but I guess it trims extraneous crap off or something)..........z well ' I' is not equal to 'I'.... because space is considered while you compare. so when you do comparision you have to trim off all exta spaces from left and right side of your string. Ok, I guess the explode added a space or something...maybe because the I was at the end of the record or something....... Thank you again!!!!!!!!!!!!!!!!!!!!!...................z Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted January 7, 2008 Share Posted January 7, 2008 Chances are there was a newline character after the I. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.