PHP implementation Read and Write tab-split files _php tutorial

Source: Internet
Author: User

PHP implementation reads and writes tab-separated files


This article describes the PHP implementation of Read and write tab segmentation files. Share to everyone for your reference. The specific analysis is as follows:

This PHP code implements reading and writing tab-separated files, containing two separate functions, one read, one write, such as CVS files, etc.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

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 is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/1009822.html www.bkjia.com true http://www.bkjia.com/PHPjc/1009822.html techarticle PHP Implementation of Read and write tab-separated files in this paper, we describe the PHP implementation of reading and writing tab segmentation files. Share to everyone for your reference. The specific analysis is as follows: This PHP code real ...

  • 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.