Jump to content

Recommended Posts

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);

Link to comment
https://forums.phpfreaks.com/topic/270282-having-issues-with-a-code/
Share on other sites

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 by MMDE

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 by MMDE

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 by MMDE

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.

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 by MMDE

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 by MMDE
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.