如何将文本文件中的两到三个单词与 PHP 中的空格匹配
How to match two to three words in a text file with whitespace in PHP
我对 PHP 有点陌生,但几乎已经创建了我需要的所有东西,除了一件事让我来到这里。我正在为一个朋友创建一个简单的(可能适合你但不适合我)网站,这样他们就可以跟踪他们何时收到租金、输入新租户并检查当前余额。我的一切都很完美……我想。在查看余额时,我测试了只输入一个单词来读取文本文件并输出正确的信息。但是,我发现如果名字和姓氏之间存储的是 space,我将无法匹配。我需要代码来完整读取输入的租户名称以及 space 之间的内容。我还没有找到任何我能真正理解的东西,并且一直在寻找几个晚上。这是我用于搜索和检索结果的完整代码。
正在搜索的文本文件是这样的:
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>
提前感谢您的帮助。
只需像这样urldecode()
:
$search = urldecode($_POST["name"]);
之后你应该会没事的。
我对 PHP 有点陌生,但几乎已经创建了我需要的所有东西,除了一件事让我来到这里。我正在为一个朋友创建一个简单的(可能适合你但不适合我)网站,这样他们就可以跟踪他们何时收到租金、输入新租户并检查当前余额。我的一切都很完美……我想。在查看余额时,我测试了只输入一个单词来读取文本文件并输出正确的信息。但是,我发现如果名字和姓氏之间存储的是 space,我将无法匹配。我需要代码来完整读取输入的租户名称以及 space 之间的内容。我还没有找到任何我能真正理解的东西,并且一直在寻找几个晚上。这是我用于搜索和检索结果的完整代码。
正在搜索的文本文件是这样的:
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>
提前感谢您的帮助。
只需像这样urldecode()
:
$search = urldecode($_POST["name"]);
之后你应该会没事的。