(事先說一句抱歉的長期問題 - 這個問題其實很簡單 - 但要解釋它也許不是那麼簡單)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