0

我正在尝试从目录中的文件中读取文本,并将其显示为图像下方的描述文本。我已经能够使用 STRIPOS 函数来分离文本的每个部分,除了最后一部分有问题。最后一部分的标题是“描述:”,实际上是多行。我不知道如何显示更多内容,而不仅仅是显示“描述:”的行。我想从“描述:”打印到文件末尾。我将在此消息中发布我的代码和文本文件。

$dirname = 'data';

$dirhandle = opendir($dirname);

$housestextarray = array();

if ($dirhandle)
        {
            while (false !==($file = readdir($dirhandle)))
            {
                if ($file !='.' && $file !='..')
                {
                    array_push($housestextarray, $file);
                }
            }

            closedir($dirhandle); 
        }

    sort($housestextarray);


    foreach ($housestextarray AS $housedescription)
        {
            $housetext = '';

            $description = '';

            $pos = stripos($housedescription, 'house_');

            if ($pos === false)
            {
                //nothing
            } else {
                    $lines_in_file = count(file($housedescription));

                    $fp=fopen($housedescription,"r");

                    for ($cntr = 1; $cntr <= $lines_in_file; $cntr++)
                {
                    $cityline=fgets($fp);
                    $priceline=fgets($fp);
                    $bedroomsline=fgets($fp);
                    $bathsline=fgets($fp);
                    $footageline=fgets($fp);
                    $realtorline=fgets($fp);
                    $grabberline=fgets($fp);

                    $descriptionline=fgets($fp);

                    //print $cityline;
                    //print $descriptionline;

                    //$housetext .= $line;

                    $citypos = stripos($cityline, 'City:');

                    if ($citypos === false)  //found the city line first time
                    {
                        //nothing
                    }
                    else
                    {
                        $city= $cityline."<br />\n";
                        //print $city;
                    } 

                    $pricepos = stripos($priceline, 'Price:');

                    if ($pricepos === false)  //found the city line first time
                    {
                        //nothing
                    }
                    else
                    {
                        $price = $priceline."<br />\n";
                        //print $price;
                    } 

                    $bedroomspos = stripos($bedroomsline, 'Bedrooms:');

                    if ($bedroomspos === false)  //found the city line first time
                    {
                        //nothing
                    }
                    else
                    {
                        $bedrooms = $bedroomsline."<br />\n";
                        //print $bedrooms;
                    } 

                    $bathspos = stripos($bathsline, 'Baths:');

                    if ($bathspos === false)  //found the city line first time
                    {
                        //nothing
                    }
                    else
                    {
                        $baths = $bathsline."<br />\n";
                        //print $baths;
                    } 

                    $footagepos = stripos($footageline, 'Footage:');

                    if ($footagepos === false)  //found the city line first time
                    {
                        //nothing
                    }
                    else
                    {
                        $footage = $footageline."<br />\n";
                        //print $footage;
                    } 

                    $realtorpos = stripos($realtorline, 'Realtor:');

                    if ($realtorpos === false)  //found the realtor line first time
                    {
                        //nothing
                    }
                    else
                    {
                        $realtor = $realtorline."<br />\n";
                        //print $realtor;
                    } 

                    $grabberpos = stripos($grabberline, 'Grabber:');

                    if ($grabberpos === false)  //found the grabber line first time
                    {
                        //nothing
                    }
                    else
                    {
                        $grabber_formatted = str_replace('Grabber:','', $grabberline);
                        $grabber = "<h3>".$grabber_formatted."</h3><br />\n";
                        //print $grabber;
                    } 

                    $descriptionpos = stripos($descriptionline, 'Description: ');
                                                                    if ($descriptionpos === false)  //found the description line first time
                    {
                        //nothing
                    }
                    else
                    {
                        $description .= $descriptionline."<br />";

                        //print $description;

                    }
                }
                    $output = $grabber."<br/>".$city.$bedrooms.$baths;
                    $output .= $price.$footage.$realtor."<br />";
                    $output .= "<br />".$description."<br />";

                    print $output;

            }   

这是文本文件内容示例(六个文件之一):

城市:OceanCove
价格:950,000 美元
卧室:5
浴室:3
影片:3000 平方英尺
房地产经纪人:Shirley Urkiddeng
Grabber:梦幻之家,美景!
描述:
您永远不会厌倦在客厅沙发上观看日落或在后门廊欣赏日出,俯瞰美丽的珊瑚峡谷。千载难逢的机会!

在 Branden 的帮助下更新代码:

    function houseDescriptions()
{
    //$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
    //$dirname = $DOCUMENT_ROOT.'data';
$dirname = 'data';

$dirhandle = opendir($dirname);

$housestextarray = array();

if ($dirhandle)
        {
            while (false !==($file = readdir($dirhandle)))
            {
                if ($file !='.' && $file !='..')
                {
                    array_push($housestextarray, $file);
                }
            }

            closedir($dirhandle); 
        }

    sort($housestextarray);


    foreach ($housestextarray AS $housedescription)
        {
                        $housetext = '';

                        $description = '';

                        $data ="";

                        $pos = stripos($housedescription, 'house_');

                        if ($pos === false)
                        {
                            //nothing
                        } else {
                            $file_handle = fopen($housedescription, "r");
                    $data = "";
                    while (!feof($file_handle)) {
                       $filestrings .= fgets($file_handle);
                    }
                    fclose($file_handle);

                    //You'll need to double check the Regex if it doesn't work.
                    $data = preg_split('#\b(City:|Bedrooms:|Baths:|Footage:|Realtor:|Grabber:|Description:)\b#', $filestrings);

                    $city = $data[0];
                    $bedrooms = $data[1];
                    $baths = $data[2];
                    $footage = $data[3];
                    $realtor = $data[4];
                    $grabber = $data[5];
                    $description = $data[6];                    
                    $output = $grabber."<br />".$city.$bedrooms.$baths;
                    $output .= $price.$footage.$realtor."<br />";
                    $output .= "<br />".$description."<br />";

                    print $output;

            }   

        }
        //return $output;
}
4

1 回答 1

0

用正确的正则表达式更新

fgets 在尝试从文本文件中获取字符串时可能会出现问题,尤其是长字符串,其中可能存在您在文本编辑程序中看不到的换行符。更好的方法是从文本文件中获取所有信息并将其存储在一个字符串中,然后使用preg_split()处理所有搜索。下面的代码(假设我的正则表达式是正确的,您可能需要重新检查它)将为您从文本文件中获取所有变量。

//Append the file location to the file name
$housedescription = $dirname."/".$housedescription;

//Get all contents of the file and save them into $filestrings
$file_handle = fopen($housedescription, "r");
$data = "";
while (!feof($file_handle)) {
   $filestrings .= fgets($file_handle);
}
fclose($file_handle);

//Remove any pesky \n newlines that were added by the text file
$filestring = preg_replace("/[\n\r]/","",$filestrings); 

//Case Sensitive Regex Search, split the string into an array based on the keywords
$data = preg_split('/(City:|Price:|Bedrooms:|Baths:|Footage:|Realtor:|Grabber:|Description:)/', $filestring, -1, PREG_SPLIT_NO_EMPTY);

//Save all the keywords to vars
$city = "City: ".$data[0];
$bedrooms = "Bedrooms: ".$data[1];
$baths = "Baths: ".$data[2];
$footage = "Footage: ".$data[3];
$realtor = "Realtor: ".$data[4];
$grabber = "Grabber: ".$data[5];
$description = "Description: ".$data[6];

注意添加了 $filestring = preg_replace("/[\n\r]/","",$filestrings); 这将删除文本文件中任何额外的新行,因此没有
您不想要它们的地方。

当然,绝对理想的做法是将所有数据存储在 mysql 数据库而不是 .txt 文件中,因为它更安全,数据访问速度更快。但是,如果您更喜欢 .txt,请尽量不要多次打开同一个文件。

一些注意事项:如何使用正则表达式fopenfgetspreg_replacepreg_split

于 2012-07-30T18:56:26.443 回答