我正在尝试从目录中的文件中读取文本,并将其显示为图像下方的描述文本。我已经能够使用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
价格:95万美元
卧室:5间
浴缸:3
占地面积:3000平方英尺
房地产经纪人:Shirley Urkiddeng
掠夺者:梦幻般的家,拥有梦幻般的景色
描述:
你永远不会厌倦看日落从客厅沙发或日出从后廊俯瞰美丽的珊瑚峡谷。一生只有一次机会
在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;
}
使用正确的正则表达式更新
fgets在试图从文本文件中获取字符串时可能会出现问题,尤其是长字符串,其中可能有换行符,您可能在文本编辑程序中看不到。更好的方法是从文本文件中获取所有信息并将其存储在字符串中,然后使用preg_split()处理所有搜索。下面的代码(假设我的regex是正确的,您可能需要重新检查它)将为您从文本文件中获取所有变量。
//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("/[nr]/","",$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,尽量不要多次打开同一个文件。
请注意:如何使用正则表达式、fopen、fgets、preg_replace、preg_split