Subversion Repositories cheapmusic

Rev

Rev 18 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
18 - 1
<?php
2
 
3
class simple_xml_extended extends SimpleXMLElement
4
{
5
    public    function    Attribute($name)
6
    {
7
        foreach($this->Attributes() as $key=>$val)
8
        {
9
            if($key == $name)
10
                return (string)$val;
11
        }
12
    }
13
 
14
}
15
 
44 - 16
function fputcsv2 ($fh, array $fields, $delimiter = ',', $enclosure = '"', $mysql_null = true) {
18 - 17
    $delimiter_esc = preg_quote($delimiter, '/');
18
    $enclosure_esc = preg_quote($enclosure, '/');
19
 
20
    $output = array();
21
    foreach ($fields as $field) {
44 - 22
        if (($field === null || empty($field)) && $mysql_null) {
18 - 23
            $output[] = 'NULL';
24
            continue;
25
        }
26
 
27
        $output[] = preg_match("/(?:${delimiter_esc}|${enclosure_esc}|\s)/", $field) ? (
28
            $enclosure . str_replace($enclosure, $enclosure . $enclosure, $field) . $enclosure
29
        ) : $field;
30
    }
31
 
32
    fwrite($fh, join($delimiter, $output) . "\n");
33
}
34
 
35
// the _EXACT_ LOAD DATA INFILE command to use
36
// (if you pass in something different for $delimiter
37
// and/or $enclosure above, change them here too;
38
// but _LEAVE ESCAPED BY EMPTY!_).
39
/*
40
LOAD DATA INFILE
41
    '/path/to/file.csv'
42
 
43
INTO TABLE
44
    my_table
45
 
46
FIELDS TERMINATED BY
47
    ','
48
 
49
OPTIONALLY ENCLOSED BY
50
    '"'
51
 
52
ESCAPED BY
53
    ''
54
 
55
LINES TERMINATED BY
56
    '\n'
57
*/
58
 
59
 
60
// get uncompressed file size for a gzip file
61
function gzfilesize($filename) {
62
  $gzfs = FALSE;
63
  if(($zp = fopen($filename, 'r'))!==FALSE) {
64
    if(@fread($zp, 2) == "\x1F\x8B") { // this is a gzip'd file
65
      fseek($zp, -4, SEEK_END);
66
      if(strlen($datum = @fread($zp, 4))==4)
67
        extract(unpack('Vgzfs', $datum));
68
    }
69
    else // not a gzip'd file, revert to regular filesize function
70
      $gzfs = filesize($filename);
71
    fclose($zp);
72
  }
73
  return($gzfs);
74
}
75
 
76
// delete trailing "(0-9)" string from discogs name fields
77
function cleanName($s) {
78
 return trim(preg_replace('/\s*\([^)]*\)$/', '', $s));
79
}
80
?>