循环行输出PHP-连接的FGET

循环行输出PHP-连接的FGET,php,Php,我正在一个测试网站上工作,该网站在搜索中提供类似房地产的信息。除了房屋的图像,还有通过目录访问引入的描述。除了“Description:”行之外,我的所有行都正常工作。在文本文件中,除“说明:”类别在标题行之后运行并分成多行,直到文件末尾外,每个类别都用行分隔。我使用连接的FGET获取信息,但在我的结果中,输出正确地显示了完整的房屋信息,包括“描述:”行,但在每个后续结果中,房屋信息重复先前打印的内容,然后重复下一个文件的其余部分。如果我去掉连接,只做一个常规的: $pos = stripos(

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

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

if($pos !== false)
{
$description = substr($line, 12);
$description = trim($description);
}
然后,我只打印了“Description:”而不打印Description部分的文本

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

城市:海洋湾
价格:$950000
卧室:5间 浴室:3个
镜头:3000平方英尺
房地产经纪人:雪莉·乌尔基登
格拉伯:拥有绝佳视野的绝佳家园
说明:
你永远不会厌倦看日落 从你的客厅沙发或日出 从你的后廊可以俯瞰 美丽的珊瑚峡谷。一生一次 机会

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

     <?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;
                }

            }
        }
$pos=stripos($line,'Description:');
如果($pos!==false)
{
$descriptionFlag=“Y”;
}
如果($descriptionFlag=='Y')
{
如果(!feof($fp))
{
$description.=$line.“
\n”; 如果(联邦储备基金($fp)) { 打破 } } }
由于
说明
可能包含多行内容,因此您必须在这些行上循环,直到完成
说明
字段。如果同一文件中有多个项目,您可能需要搜索下一个
城市
,以便进行救援。例如:

if($descriptionFlag=='Y') {
  while ($line = fgets($handle, 8192)) {
      $line = trim($line);
      if($line === 'City')
        break;
      $description = $description . " " . $line;
  }
  $description = $description . "<br />\n";
}
if($descriptionFlag=='Y'){
而($line=fgets($handle,8192)){
$line=修剪($line);
如果($line=='City')
打破
$description=$description.“.”行;
}
$description=$description.“
\n”; }
试试看

if($descriptionFlag=='Y'){
而($line=fgets($handle,8192)){
$line=修剪($line);
if(substr($line,0,4)=='City')
打破
$description=$description.“.”行;
}
$description=$description.“
\n”; }
无法实现这一点。我想知道是否有一种方法可以像我现在这样使用它,并让它识别feof?我尝试了上面在新代码部分中添加的内容。您添加的代码检查feof是否正常,但它不会
循环
描述内容。尝试使用我的代码并仅更改行:
如果($line=='City')
to:
如果(feof($fp))
会尝试,那么“8192”代表什么?
if($descriptionFlag=='Y') {
  while ($line = fgets($handle, 8192)) {
      $line = trim($line);
      if(substr($line,0,4) === 'City')
        break;
      $description = $description . " " . $line;
  }
  $description = $description . "<br />\n";
}