(事先说一句抱歉的长期问题 - 这个问题其实很简单 - 但要解释它也许不是那么简单)PHP - 合并两个TXT文件与条件
我在PHP noobie技能是通过这个挑战:
输入的2用这样的结构TXT文件:
$rowidentifier //number,letter,string etc..
$some semi-fixed-string $somedelimiter $semi-fixed-string
$content //with unknown length or strings or lines number.
阅读上述,我的“半固定字符串的含义,意味着它是与已知结构的字符串,但是未知的内容..
给一个实际的例子,let's采取SRT文件(我只是把它作为一个豚鼠的结构非常相似,我需要):
1
00:00:12,759 --> 00:00:17,458
"some content here "
that continues here
2
00:00:18,298 --> 00:00:20,926
here we go again...
3
00:00:21,368 --> 00:00:24,565
...and this can go forever...
4
.
.
.
我想要做的,是从一个文件中获取$ content部分,并将其放在第二个文件的正确位置。
要回示例SRT,具有:
//file1
1
00:00:12,759 --> 00:00:17,458
"this is the italian content "
which continues in italian here
2
00:00:18,298 --> 00:00:20,926
here we go talking italian again ...
和
//file2
1
00:00:12,756 --> 00:00:17,433
"this is the spanish, chinese, or any content "
which continues in spanish, or chinese here
2
00:00:16,293 --> 00:00:20,96
here we go talking spanish, chinese or german again ...
将导致
//file3
1
00:00:12,756 --> 00:00:17,433
"this is the italian content "
which continues in italian here
"this is the spanish, chinese, or any content "
which continues in spanish, or chinese here
2
00:00:16,293 --> 00:00:20,96
here we go talking italian again ...
here we go talking spanish, chinese or german again ...
或多个PHP等:
$rowidentifier //unchanged
$some semi-fixed-string $somedelimiter $semi-fixed-string //unchanged, except maybe an option to choose if to keep file1 or file2 ...
$content //from file 1
$content //from file 2
所以,这一切后引入 - 这是我有什么(这相当于没有实际..)
$first_file = file('file1.txt'); // no need to comment right ?
$second_file = file('file2.txt'); // see above comment
$result_array = array(); /construct array
foreach($first_file as $key=>$value) //loop array and....
$result_array[]= trim($value).'/r'.trim($second_file[$key]); //..here is my problem ...
// $Value is $content - but LINE BY LINE , and in our case, it could be 2-3- or even 4 lines
// should i go by delimiters /n/r ?? (not a good idea - how can i know they are there ??)
// or should i go for regex to lookup for string patterns ? that is insane , no ?
$fp = fopen('merge.txt', 'w+'); fwrite($fp, join("\r\n", $result_array); fclose($fp);
这将通过线做线 - 这是不是我所需要的。我需要的条件.. 也 - 我相信这不是一个聪明的代码,或者有很多更好的方法去 - 所以任何帮助,将不胜感激...
用'把它放在正确的place'你的意思是交替行?或者是什么? – Jon
我的意思是只放一个文件的内容$零件文件2的$内容后,没有influancing其他部件或结构...... – krembo99