The PHP implementation intercepts the N-character method that starts at a location in the GBK document

Source: Internet
Author: User
Tags fread
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.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.