<?php
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$file = file_get_contents('directory.txt');
$searchfname = stripos($file, $fname);
$searchlname = stripos($file, $lname);
$lines = file('directory.txt');
header('Content-Type: text/plain');
foreach($lines as $line){
if (($searchfname !== false) && ($searchlname !== false)){
echo $line;
}
}
?>
我当前的代码。我所面临的问题是,它回声的整个文本文件。如何使它只打印与$fname和$lname匹配的行?
John Doe daahl92@gmail.com 57123466930 1192 Broad Pl Herndon AR 20170.
dave attard hockyren97@aim.com 235626930 dfgdfg Reston AK 20171.
jason Green daahl97@gmail.com 571245930 1192 Way Pl Reston TN 20176.
这是我的文本文件。
<?php
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip= $_POST['zip'];
$fh = fopen("directory.txt", "a+");
fwrite($fh, "$fname $lname $email $phone $address $city $state $zipn");
fclose($fh);
?>
这是一个php文件,用来用联系人填充我的文本文件。当我尝试回显某一行时,它会回显所有行。
您将需要简单地循环遍历数组并检查stripos是否返回NON false结果。我把名字和姓氏合并成一个字符串,因为你希望他们在一起。我添加了一个exit,以便在找到结果时退出循环。
<?php
$fname = "jason";
$lname = "Green";
$needle = "$fname $lname";
$lines = file('directory.txt');
foreach($lines as $line){
if(stripos($line,$needle) !== false){
echo "Line: $line";
exit;
}
}
?>