gpt4 book ai didi

php - 重复行输出 PHP - 连接的 fgets

转载 作者:搜寻专家 更新时间:2023-10-31 21:14:36 25 4
gpt4 key购买 nike

我正在开发一个测试网站,该网站会在搜索时显示类似房地产的信息。除了房屋图像外,还有通过目录访问引入的描述。除了“描述:”行外,我所有的行都正常工作。在文本文件中,每个类别都由行分隔,除了“描述:”类别在标题行之后运行并分成多行直到文件末尾。我正在使用串联的 fgets 来获取信息,但在我的结果中,输出正确显示了完整的房屋信息,包括“描述:”行,但在每个后续结果中,房屋信息重复了之前打印的内容,然后是其余部分下一个文件。如果我摆脱连接并只是做一个常规的:

$pos = stripos($line, 'Description:');

if($pos !== false)
{
$description = substr($line, 12);
$description = trim($description);
}

然后我最终只打印了“Description:”,没有描述部分的文本。

这是导入文本文件的示例:

城市:OceanCove
售价:950,000 美元
卧室:5
浴室:3
建筑面积:3000 平方英尺
房地产经纪人:Shirley Urkiddeng
Grabber:拥有美景的梦幻家园!
描述:
你永远不会厌倦看日落从你的客厅沙发或日出从你的后门廊可以俯瞰美丽的珊瑚峡谷一生一次的机会!

这是我的代码(在“描述”部分有一些注释掉了我所做的其他试验:

     <?php
$findhome = $_POST['findhome'];

$header = getHeader($findhome);
print $header;

getResults($findhome);

function getHeader($findhome)
{
if (empty($findhome))
{
$header = "<h3>Current Listings: <br />";
}
else
{
$header = "<h3>Current Listings that match: $findhome </h3><br />";
}

return $header;

}

function getResults($findhome)
{


if (empty($findhome))
{
$findhome ='ALL';
}

$dirname = 'images';

$dirhandle = opendir($dirname);

if ($dirhandle)
{
$houseimagesarray = array();

while (false !== ($file = readdir($dirhandle)))
{
if ($file !='.' && $file !='..')
{
$first_6 = substr($file,0,6);

if($first_6 =='house_')
{
array_push($houseimagesarray, $file);
}
}
}
}

sort($houseimagesarray);

$description = '';

foreach ($houseimagesarray as $image_filename)
{


//***************************************************
//** Function Definitions
//***************************************************

//Get Image Files

$imagename ='images/'.$image_filename; //.jpg files
$house_img = "<p><img src='".$imagename."'></p>";

$houseinfo_filename = str_replace ('.jpg', '.txt', $image_filename);

$filename = 'data/'.$houseinfo_filename; //.txt file

$fp = fopen($filename, 'r');


//Get Image House Information


$show_house = 'Y'; //Set default value

while(true)
{
$line = fgets($fp);

if (feof($fp))
{
break;
}

$pos = stripos($line, 'City:');

if ($pos !== false)
{
$city = substr($line, 5);
$city = trim($city);

if ($findhome != 'ALL')
{
$subpos = stripos($city, $findhome);

if($subpos === false)
{
$show_house = 'N';
break;
}
}
}

$pos = stripos($line, 'Price:');

if ($pos !==false)
{
$price = substr($line, 6);
$price = trim($price);
}

$pos = stripos($line, 'Bedrooms:');

if ($pos !== false)
{
$bedrooms = substr($line, 9);
$bedrooms = trim($bedrooms);
}

$pos = stripos($line, 'Baths:');

if ($pos !== false)
{
$baths = substr($line, 6);
$baths = trim($baths);
}

$pos = stripos($line, 'Footage:');

if($pos !== false)
{
$footage = substr($line, 8);
$footage = trim($footage);
}

$pos = stripos($line, 'Realtor:');

if($pos !== false)
{
$realtor = substr($line, 8);
$realtor = trim($realtor);
}

$pos = stripos($line, 'Grabber:');

if($pos !== false)
{
$grabber = substr($line, 8);
$grabber = trim($grabber);
}

$pos = stripos($line, 'Description:');

if($pos !== false)
{
$descriptionFlag = "Y";
}
if($descriptionFlag=='Y')
{
$description .=$line."<br />\n";
//$description =$line."<br />";
//$description = $description.$line."<br />";
}
}

if ($show_house == 'Y')
{
print $house_img;

print "<h3>".$grabber."</h3><br />";
print "City: ".$city."<br />";
print "Bedrooms: ".$bedrooms."<br />";
print "Baths: ".$baths."<br />";
print "Price: ".$price."<br />";
print "Footage: ".$footage."<br />";
print "Realtor: ".$realtor."<br />";

print $description;

}


}
}
?>

新代码:

    $pos = stripos($line, 'Description:');

if($pos !== false)
{
$descriptionFlag = "Y";
}
if($descriptionFlag=='Y')
{
if(!feof($fp))
{
$description .=$line."<br />\n";

if(feof($fp))
{
break;
}

}
}

最佳答案

由于 Description 可能包含不止一行,因此您必须遍历这些行,直到完成 Description 字段。如果您在同一个文件中有多个项目,您可能需要搜索下一个 City 以便摆脱困境。像这样的东西:

if($descriptionFlag=='Y') {
while ($line = fgets($handle, 8192)) {
$line = trim($line);
if($line === 'City')
break;
$description = $description . " " . $line;
}
$description = $description . "<br />\n";
}

关于php - 重复行输出 PHP - 连接的 fgets,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11784285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com