This article mainly introduces the implementation of PHP to intercept GBK document at the beginning of a certain location of the N-character method, interested in a friend's reference, I hope to be helpful to everyone.
cut.php:
#!/usr/bin/php<?phpdefine (' input_file ', ' t.txt ');d efine (' output_file ', ' a.txt '); $pos = max (Intval ($argv [1]), 0); $len = Max (Intval ($argv [2]), 0); $file _size = filesize (input_file), if ($pos >= $file _size) exit; $fp = fopen (input_file, ' RB '); $point = 0; Current byte position$string = '; while (Ftell ($FP) < $file _size) { if ($point >= $pos + $len) break; $byte = Fread ($fp, 1); PHP version >= 5.4 $char = unpack (' C ', $byte) [1]; if ($char <= 0x7f) { //single byte if ($point >= $pos) $string. = $byte; $point + = 1; Continue; } else { //double bytes if ($point >= $pos) { $string. = $byte. Fread ($fp, 1); } else { fseek ($fp , 1, seek_cur); } $point + = 1; Continue; } } Fclose ($FP); File_put_contents (Output_file, $string);? >
Source File T.txt content:
Dkei20 Wang Nnso
Test command:
./cut.php 6 1
View results:
Hexdump-c t.txt && hexdump-c a.txt
The above is the whole content of this article, I hope that everyone's study has helped.