如何在PHP中选择目录中的部分文本文件并使用STRIPOS打印部分?

如何在PHP中选择目录中的部分文本文件并使用STRIPOS打印部分?,php,arrays,stripos,Php,Arrays,Stripos,我试图从目录中的文件中读取文本,并将其显示为图像下方的描述文本。我已经能够使用STRIPOS函数来分离文本的每个部分,但最后一部分有问题。最后一节的标题是“描述:”并且实际上包含多行内容。我不知道如何显示的不仅仅是“Description:”这一行。我想从“Description:”打印到文件的末尾。我将在此消息中发布我的代码和文本文件 $dirname = 'data'; $dirhandle = opendir($dirname); $housestextarray = array();

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

$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;

            }   
$dirname='data';
$dirhandle=opendir($dirname);
$housestextarray=array();
如果($dirhandle)
{
while(false!=($file=readdir($dirhandle)))
{
如果($file!='.&&$file!='..'))
{
array\u push($housestextarray,$file);
}
}
closedir($dirhandle);
}
排序($housestextarray);
foreach($housestextarray作为$housesdescription)
{
$housetext='';
$description='';
$pos=stripos($house description,'house_');
如果($pos==false)
{
//没什么
}否则{
$lines_in_file=计数(文件($housescription));
$fp=fopen($House Description,“r”);
对于($cntr=1;$cntr使用适当的正则表达式更新
fgets在试图从文本文件中获取字符串时可能会出现问题,尤其是长字符串,其中可能有在文本编辑程序中可能看不到的换行符。更好的方法是从文本文件中获取所有信息并将其存储在字符串中,然后使用以下代码处理所有搜索(假设我的正则表达式是正确的,您可能需要重新检查它)将为您从文本文件中获取所有变量

//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\u replace(“/[\n\r]/”,“,$filestrings”);这将删除文本文件中的任何额外新行,这样就不会有不需要它们的

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


需要注意的是:,,

谢谢Branden!虽然我对所有这些都不熟悉,并且我将代码转入了,但得到了一个无休止的循环错误:警告:feof()希望参数1是资源,布尔值在第104行的/Applications/MAMP/htdocs/homelist2.php中给出警告:fgets()预期参数1为resource,布尔值在/Applications/MAMP/htdocI中给出,更新了上面的问题,以显示我输入的代码与您提供给我的匹配。它似乎也没有被标记分开。如果我打印$city,我会得到文本中的所有内容以及重复多次的文本,其中相同的文件会被重新输出t稍后在下一个文件的输出之间重复…没有明确的模式。如果我正确读取代码,$housescription应该等于文本文件名的字符串,对吗?那么$housescription应该等于说house_1.txt正确吗?我不能100%确定我在代码“#\b”中提供的正则表达式(城市:|卧室:|浴室:|镜头:|房地产经纪人:|抓取者:|描述:|)\b#“这就解释了为什么要取回整个文件的内容。preg|u 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];