ank101302 Posted November 4, 2012 Share Posted November 4, 2012 The following code is giving me some problems. It is adding an extra 0:00 at the end of the list and I need to remove it but I have tried everything I can think of and it is not working. If anyone can please help me out that would be greatly appreciated. Thanks! $fh=fopen("datebook.txt", "r"); while ( ! feof($fh)){ $text = fgets($fh); list($name, $phNum, $address, $date, $income)=preg_split("/:/", $text); $raise=preg_replace("/(\d+)/e", "$1+250", $income); echo "$name: "; printf("%.2f\n", $raise);//reads one line to far. How to end it at last line? } fclose($fh); Quote Link to comment https://forums.phpfreaks.com/topic/270282-having-issues-with-a-code/ Share on other sites More sharing options...
MMDE Posted November 4, 2012 Share Posted November 4, 2012 (edited) I recommend using these two methods: http://php.net/manua...et-contents.php http://www.php.net/m...ut-contents.php Assign the content of the file to a variable. Parse the variable with various methods you'd like. If you want to parse one line at a time, then you may want to use these two functions to create an array of it and then back to a string: http://php.net/manua...ion.explode.php http://php.net/manua...ion.implode.php Are you sure you need to use preg_split? Explode should be enough for you I think. I will try to understand what the problem is now. Actually, try what I suggested above first, then tell us if there is still any problems afterwards. If there are, I would suggest printing to screen all the data in the various arrays you create using print_f($the_array). When using print_f and an array, you should check the HTML source, as it's much easier to read then, because it places a new value in the array at a new line, and your webbrowser ignores it as it's not a br tag. Edited November 4, 2012 by MMDE Quote Link to comment https://forums.phpfreaks.com/topic/270282-having-issues-with-a-code/#findComment-1390138 Share on other sites More sharing options...
ank101302 Posted November 4, 2012 Author Share Posted November 4, 2012 Yes I have to use the preg_split for this one. Wish I didn't, I could have done it then. Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/270282-having-issues-with-a-code/#findComment-1390140 Share on other sites More sharing options...
MMDE Posted November 4, 2012 Share Posted November 4, 2012 (edited) Yes I have to use the preg_split for this one. Wish I didn't, I could have done it then. Thanks for your help. Are you sure you can't just do this instead: list($name, $phNum, $address, $date, $income)=explode(':', $text); ? Edited November 4, 2012 by MMDE Quote Link to comment https://forums.phpfreaks.com/topic/270282-having-issues-with-a-code/#findComment-1390141 Share on other sites More sharing options...
ank101302 Posted November 4, 2012 Author Share Posted November 4, 2012 Gave it a try but the extra 0:00 is still hanging in there. Quote Link to comment https://forums.phpfreaks.com/topic/270282-having-issues-with-a-code/#findComment-1390143 Share on other sites More sharing options...
MMDE Posted November 4, 2012 Share Posted November 4, 2012 (edited) Gave it a try but the extra 0:00 is still hanging in there. $t = file_get_contents('datebook.txt'); $ta = explode("\n", $t); $tal = count($ta); for($i=0; $i<$tal; $i++){ $la = explode(':', $ta[$i]); $raise = preg_replace("/(\d+)/e", "$1+250", $la[4]); printf("%s: %.2f\n", $ta[0], $raise); //print_f($ta[$i]); //print_f($la); } The above is what I would have done, but if this doesn't work, please give us an example of the file you get your contents from. Edited November 4, 2012 by MMDE Quote Link to comment https://forums.phpfreaks.com/topic/270282-having-issues-with-a-code/#findComment-1390144 Share on other sites More sharing options...
ank101302 Posted November 4, 2012 Author Share Posted November 4, 2012 This is an example of the content from the file: Jennifer Cowan:548-555-2348:583 Laurel Ave., Kingsville, TX 83745:10/1/35:58900 For the list I need just the name and new income level. The income is the last set of numbers after the ":" If I cannot get the 0:00 I can just leave it and ignore it as I go. I am more or less just trying to figure out what would cause it to add the extra line in. Quote Link to comment https://forums.phpfreaks.com/topic/270282-having-issues-with-a-code/#findComment-1390145 Share on other sites More sharing options...
MMDE Posted November 4, 2012 Share Posted November 4, 2012 (edited) Try this code: <?php $t = file_get_contents('datebook.txt'); $ta = explode("\n", $t); $tal = count($ta); for($i=0; $i<$tal; $i++){ $la = explode(':', $ta[$i]); if(count($la)!=5){ echo 'Error on line '.($i+1).': "'.$ta[$i]."\"\n"; continue; } $raise = preg_replace("/(\d+)/e", "$1+250", $la[4]); printf("%s: %.2f\n", $ta[0], $raise); } ?> EDIT: Sorry, I edited it for a short while, but what I did in that edit was dumb, so I changed it back. Edited November 4, 2012 by MMDE Quote Link to comment https://forums.phpfreaks.com/topic/270282-having-issues-with-a-code/#findComment-1390148 Share on other sites More sharing options...
MMDE Posted November 4, 2012 Share Posted November 4, 2012 (edited) Try this: <?php $t = file_get_contents('datebook.txt'); $ta = explode("\n", $t); $tal = count($ta); for($i=0; $i<$tal; $i++){ $la = explode(':', $ta[$i]); if(count($la)!=5){ echo 'Error on line '.($i+1).': "'.$ta[$i]."\"\n"; continue; } $raise = round(preg_replace("/(\d+)/e", "$1+250", $la[4])*100)/100; echo $la[0].': '.$raise."\n"; } ?> I did a little fix. The 0.00 should not appear now, and you should still get double decimal precision. If you want to ignore the empty lines, you can ignore it like this at the beginning of the loop: if($ta[$i]==='') continue; If you don't want any error message at all when the line is not perfectly formatted, just remove the echo part with the error message, and just use the continue there instead. Edited November 4, 2012 by MMDE Quote Link to comment https://forums.phpfreaks.com/topic/270282-having-issues-with-a-code/#findComment-1390153 Share on other sites More sharing options...
ank101302 Posted November 4, 2012 Author Share Posted November 4, 2012 Thank you so much. That works perfect now. Quote Link to comment https://forums.phpfreaks.com/topic/270282-having-issues-with-a-code/#findComment-1390154 Share on other sites More sharing options...
ank101302 Posted November 4, 2012 Author Share Posted November 4, 2012 I took out the error message. Your last mod to it did not leave the decimal places, but it worked just fine with your first mod. Thank you for all your help! Quote Link to comment https://forums.phpfreaks.com/topic/270282-having-issues-with-a-code/#findComment-1390158 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.