Php implements the separation of reading and writing tabs _ PHP Tutorial

Source: Internet
Author: User
Php reads and writes tab-separated files. Php reads and writes tab-separated files. This article describes how php reads and writes tab-separated files. Share it with you for your reference. The specific analysis is as follows: This php code implements the separation of reading and writing tabs in php.

The example in this article describes how php implements the separation of reading and writing tabs. Share it with you for your reference. The specific analysis is as follows:

This php code reads and writes tab-separated files, including two independent functions, one read and one write, such as cvs files.

?

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

63

64

65

66

67

//

// Save an array as tab seperated text file

//

Function write_tabbed_file ($ filepath, $ array, $ save_keys = false ){

$ Content = '';

Reset ($ array );

While (list ($ key, $ val) = each ($ array )){

// Replace tabs in keys and values to [space]

$ Key = str_replace ("\ t", "", $ key );

$ Val = str_replace ("\ t", "", $ val );

If ($ save_keys) {$ content. = $ key. "\ t ";}

// Create line:

$ Content. = (is_array ($ val ))? Implode ("\ t", $ val): $ val;

$ Content. = "\ n ";

}

If (file_exists ($ filepath )&&! Is_writeable ($ filepath )){

Return false;

}

If ($ fp = fopen ($ filepath, 'W + ')){

Fwrite ($ fp, $ content );

Fclose ($ fp );

}

Else {return false ;}

Return true;

}

//

// Load a tab seperated text file as array

//

Function load_tabbed_file ($ filepath, $ load_keys = false ){

$ Array = array ();

If (! File_exists ($ filepath) {return $ array ;}

$ Content = file ($ filepath );

For ($ x = 0; $ x <count ($ content); $ x ++ ){

If (trim ($ content [$ x])! = ''){

$ Line = explode ("\ t", trim ($ content [$ x]);

If ($ load_keys ){

$ Key = array_shift ($ line );

$ Array [$ key] = $ line;

}

Else {$ array [] = $ line ;}

}

}

Return $ array;

}

/*

** Example usage:

*/

$ Array = array (

'Line1' => array ('data-1-1 ', 'data-1-2', 'data-1-3 '),

'Line2' => array ('data-2-1 ', 'data-2-2', 'data-2-3 '),

'Line3' => array ('data-3-1 ', 'data-3-2', 'data-3-3 '),

'Line4' => 'foobar ',

'Line5' => 'Hello world'

);

// Save the array to the data.txt file:

Write_tabbed_file('data.txt ', $ array, true );

/* The data.txt content looks like this:

Line1 data-1-1 data-1-2 data-1-3

Line2 data-2-1 data-2-2 data-2-3

Line3 data-3-1 data-3-2 data-3-3

Line4 foobar

Line5 hello world

*/

// Load the saved array:

$ Reloaded_array = load_tabbed_file('data.txt ', true );

Print_r ($ reloaded_array );

// Returns the array from above

I hope this article will help you with php programming.

Examples in this article describe how to read and write tab files in php. Share it with you for your reference. The specific analysis is as follows: This php code is actually...

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.