php function ffgets fp while true fgetc fp if return return fp fopen s

 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
<?php
function ffgets($fp) {
$r = '';
while (true) {
$c = fgetc($fp);
$r.= $c;
if ($c == "\n") return $r;
}
return $r;
}
$fp = fopen('source.xml', 'r');
$nextfile_number = 1;
while (!feof($fp)) {
$line = ffgets($fp);
$nextfile_number_str = sprintf('%010d', $nextfile_number);
$path = "pages/".substr($nextfile_number_str, -1, 1)."/".substr($nextfile_number_str, -2, 1)."/".substr($nextfile_number_str, -3, 1)."/".substr($nextfile_number_str, -4, 1);
$oldumask = umask(0);
if (!is_dir($path)) {
mkdir($path, 0777, true);
}
umask($oldumask);
$nextfile_name = $path."/f".$nextfile_number.".xml";
if (strstr($line, "<page>")) {
$nfp = fopen($nextfile_name, 'w');
fwrite($nfp, $line);
} elseif (strstr($line, "</page>")){
fwrite($nfp, $line);
fclose($nfp);
$nextfile_number++;
echo $nextfile_number."\r\n";
} else {
fwrite($nfp, $line);
}
}
fclose($fp);
?>