-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_document.php
62 lines (48 loc) · 1.71 KB
/
convert_document.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
// error_reporting(E_ALL);
// ini_set('display_errors', 'On');
// Reads a whole TXT document and wraps each line with the function call
// outputting the results in a new file
$translateString = 'TranslateText';
$beforeText = "('";
$afterText = "')";
$filePath1 = 'input_document.txt'; // input file
$filePath2 = 'out_'.$filePath1; // output file
$matches = [];
$translateStringLength = strlen($translateString);
if ( fopen($filePath1, "r") !== false ) {
// file opened successfully
$file1 = fopen($filePath1, "r");
} else {
echo "Failed to open file at ".$filePath1." - Check permissions and try again.";
die();
}
if ( fopen($filePath2, "w") !== false ) {
// file opened successfully
$file2 = fopen($filePath2, "w");
} else {
echo "Failed to open or create a new file at ".$filePath2." - Check permissions and try again.";
die();
}
$file_contents = explode("\n", fread($file1, filesize($filePath1)));
for ($IDX = 0; $IDX < count($file_contents); $IDX++) {
if (isset($file_contents[$IDX]) && $file_contents[$IDX] == '') {
} else {
$line = $file_contents[$IDX];
$file_contents[$IDX] = $translateString.$beforeText.addslashes(stripcslashes($line)).$afterText;
}
}
echo count($file_contents).' Document Lines Read'.'<br><br>';
for ($IDX = 0; $IDX < count($file_contents); $IDX++) {
if (isset($file_contents[$IDX]) && $file_contents[$IDX] == '') {
} else {
fwrite($file2, $file_contents[$IDX] . PHP_EOL);
}
}
fclose($file1);
fclose($file2);
echo count($file_contents).' Document Lines Read'.'<br><br>';
echo 'Translation Function: '.$translateString.'<br>';
echo 'PHP array file: '.$filePath1.'<br>';
echo 'Text only file: '.$filePath2.'<br>';
exit;