我有一些文本文件。例如:file1.txt
和file2.txt.
的包含file1.txt
是Walk word1 in the rain
Walking in the rain is one of the most beautiful word2 experiences.
有一些条件:
- 如果有
word1
ANDword2
,我想得到这两个词之间的文本,$between
所以我会得到in the rain Walking in the rain is one of the most beautiful
。我也想得到文本word2
,$content
所以我会得到experiences
- 如果只有
word1
ORword2
(eg =Walk in the rain Walking in the rain is one of the most beautiful word1 experiences.
) 那么$between =''
并且$content
是所有文本->Walk in the rain Walking in the rain is one of the most beautiful word1 experiences.
- 如果
word2
前面是word1
example :Walk in word2 the rain Walking in the rain is one of the most word1 beautiful word1 experiences.
那么 $between = ''and
$content` 是所有的文本。
这是我的代码:
//to get and open the text files
$txt = glob($savePath.'*.txt');
foreach ($txt as $file => $files) {
$handle = fopen($files, "r") or die ('can not open file');
$ori_content = file_get_contents($files);
//count the words of text, to reach until the last word
$words = preg_split('/\s+/',$ori_content ,-1,PREG_SPLIT_NO_EMPTY);
$count = count ($words);
$word1 ='word1';
$word2 ='word2';
if (stripos($ori_content, $word1) && stripos($ori_content, $word2)){
$between = substr($ori_content, stripos($ori_content, $word1)+ strlen($word1), stripos($ori_content, $word2) - stripos($ori_content, $word1)- strlen($word1));
$content = substr($ori_content, stripos($ori_content, $word2)+strlen($word2), stripos($ori_content, $ori_content[$count+1]) - stripos($ori_content,$word2));
}
else
$content = $ori_content;
$q0 = mysql_query("INSERT INTO tb VALUES('','$files','$content','$between')") or die(mysql_error());
但我的代码仍然无法处理:
- 条件号 2(上),我得到结果 $between = 经验,它应该是 $between=''
- 条件编号 3(上)。我得到结果 $etween = the rain 在雨中行走是 word1 最美丽的 word1 体验之一,应该是 $between=''
- 如果我在 file1.txt 中得到 $between,但在 file2.txt 中没有,在数据库中的表之间,对于数据 file2.txt,它应该在之间的列中为空。但它不为空,它由其他文本文件之间填充
- 我无法说出最后一句话。
请帮助我..提前谢谢!:)