How to match two to three words in a text file with whitespace in PHP

Viewed 55

I am somewhat new to PHP but have created almost everything I need, except one thing which has led me here. I am creating a simple (for you probably but not for me) site for a friend so they can keep track of when they receive rent payments, input new tenants, and check current balances. I had everything working perfect...I thought. When checking the balance, I tested with only one word input to read the text file and output the proper info. But, what I found is if the first and last name are stored with a space between then, I am not getting a match. I need code to read the inputted tenant name in its entirety with the space between. I haven't found anything I could really comprehend and have been searching for several nights. Here is my full code for searching and retrieving results.

The text file being searched is like this:

John Doe,123 Main St,400.00,01/01/2016,
Jane Doe,124 Main St,300.00,01/01/2016,
John Doe,123 Main St,,01/03/2016,200.00
<?php
$lines = file('data.txt'); //here's the filename
?>
<center>
    <table id="historytable" width="640" border="1">
    <caption>Billing & Payment History
</caption>
<tbody>
<tr>
<td width="40">Tenant</td>
<td width="40">Address</td>
<td width="40">Date</td>
<td width="40">Rent</td>
<td width="40">Payment</td>
</tr>
<?php
$search = $_POST["name"]; //assigning a string to each piece of input data
// Store true when the text is found
$balance = 0; //assign initial value to balance
$renttotal = 0; //assign initial value to renttotals
$received = 0; //assign initial value to received
$found = false;
foreach ($lines as $line) { //this is the loop to read the txt file
{
  if(strpos($line, $search) !== false)
  {
    $found = true;
list($a,$b,$c,$d,$e) = explode(',', $line); //this assigns a variable name to each data item separated by a comma
   $renttotal+= $c; //each time it loops, it gathers the value of c adding it to itself  same for the two lines below
   $received+= $e;
   $balance = $renttotal - $received; //subtracts the final value of renttotal and received assigning the difference to balance

    $line_array = explode(",", $line);  //breaks each piece of data apart to be placed in a table  }
    echo "<tr>
<td width=40>$a</td>
<td width=40>$b</td>
<td width=40>$c</td>
<td width=40>$d</td>
<td width=40>$e</td>
</tr>";
}}}
?>
</tbody>
</table>
</center>
<BR />
<BR />
<center>
  <p>TOTAL RENTS CHARGED  $<?php echo "$renttotal"?><br />
    TOTAL PAYMENTS RECEIVED $<?php echo "$received"?><br />
    BALANCE DUE $<?php echo "$balance"?></p>

Thank you in advance for any help.

1 Answers
Related