我正在一个测试站点上工作,该站点提供房地产之类的搜索信息。除了房屋的图像外,还有通过目录访问引入的描述。我的所有行都正常工作,除了“描述:”行。在文本文件中,每个类别都由行分隔,除了“描述:”类别在标题行之后运行并分成多行直到文件末尾。我正在使用串联的 fgets 来获取信息,但在我的结果中,输出正确显示了完整的房屋信息,包括“描述:”行,但是在每个后续结果中,房屋信息都会重复之前打印的内容,然后是其余部分下一个文件。如果我摆脱串联并做一个常规:
$pos = stripos($line, 'Description:');
if($pos !== false)
{
$description = substr($line, 12);
$description = trim($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;
}
}
}