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

时间:2012-08-02 19:16:09

标签: php

我正在开发一个测试网站,提供搜索信息等房地产信息。除了房屋的图像外,还有通过目录访问引入的描述。除了“Description:”行之外,我的所有行都正常工作。在文本文件中,每个类别都按行分隔,但“描述:”类别在标题行之后运行,在多行之前运行直到文件末尾。我使用连接的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;
                }

            }
        }

2 个答案:

答案 0 :(得分:1)

由于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";
}

答案 1 :(得分:1)

尝试

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