Login   Register  
PHP Classes
elePHPant
Icontem

File: csv.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Hannes Dorn  >  CCsv  >  csv.php  >  Download  
File: csv.php
Role: ???
Content type: text/plain
Description: CCsv Class File.
Class: CCsv
Author: By
Last change:
Date: 2000-12-29 13:33
Size: 6,083 bytes
 

Contents

Class file image Download
<?php

if ( !defined( "INCLUCDED_CSV" ) )
{
define( "INCLUCDED_CSV", TRUE  );

/*
// $Archive: /iPage/V1.1/include/csv.php $
// $Author: Hannesd $
// $Date: 29.12.00 19:30 $
// $Revision: 2 $
//
// $History: csv.php $
// 
// *****************  Version 2  *****************
// User: Hannesd      Date: 29.12.00   Time: 19:30
// Updated in $/iPage/V1.1/include
//
// *****************  Version 1  *****************
// User: Hannesd      Date: 29.12.00   Time: 19:30
// Created in $/iPage/V1.1/include
//
// Description: Class for Reading and Writing Arrays from/into CSV Files
*/
Class CCsv
{
    /*
        Type:               Constructor
        Description:        Well, does nothing, just here
        Params:             Nothing
        Return:             Nothing
    */
    Function CCsv()
    {
    }

    /*
        Type:               Public
        Description:        Writes a 3 dimensional Array into a CSV File
        Params:
            sFilename       Filename, must be writeable
            aResult         3dim array
            fFieldnames     Should fieldnames (aka array keys) written as first line?
        Return:             true/false
    */
    Function Write( $sFilename, $aResult, $fFieldnames = true )
    {
        // Open File
    	if ( !( $pFile = fopen( $sFilename, "w" ) ) )
        	return( false );

		if ( is_array( $aResult ) )
        {
            // Should Fieldnames be written?
            if ( $fFieldnames )
            {
                $sBuffer = "";
                while( list( $sKey, $sValue ) = each( $aResult[ 0 ] ) )
                {
                    if ( $sBuffer != "" )
                        $sBuffer .= ";";

                    // replace '"' with '""'
                    $sKey = str_replace( '"', '""', $sKey );

                    // append to buffer
                    $sBuffer = $sBuffer . '"' . $sKey . '"';
                }
                if ( $sBuffer != "" )
                    $sBuffer .= "\n";

            	$iResult = fwrite( $pFile, $sBuffer );
            	if ( $iResult <> strlen( $sBuffer ) )
            	{
            	    fclose( $pFile );
            	    return( false );
            	}
            }

            // Do something with all rows ;)
            while( list( $sKey, $aRow ) = each( $aResult ) )
            {
                $sBuffer = "";
                while( list( $sKey, $sValue ) = each( $aRow ) )
                {
                    if ( $sBuffer != "" )
                        $sBuffer .= ";";

                    // replace '"' with '""'
                    $sKey = str_replace( '"', '""', $sValue );

                    // append to buffer
                    $sBuffer = $sBuffer . '"' . $sValue . '"';
                }
                if ( $sBuffer != "" )
                    $sBuffer .= "\n";

            	$iResult = fwrite( $pFile, $sBuffer );
            	if ( $iResult <> strlen( $sBuffer ) )
            	{
            	    fclose( $pFile );
            	    return( false );
            	}
            }

        	if ( !fclose( $pFile ) )
        	    return( false );
        }

        return( true );
    }

    /*
        Type:               Public
        Description:        Reads a CSF File into a 3 dimensional Array
        Params:
            sFilename       Filename, must be writeable
            fFieldnames     Should fieldnames (aka array keys) written as first line?
        Return:             a 3dim array
    */
    Function Read( $sFilename, $fFieldnames = true )
    {
        // Resultset löschen
        unset( $aResult );

        // Datei komplett in eine Array einlesen
        $aText = file( $sFilename );

        // Datei öffnen
		if ( is_array( $aText ) )
        {
            if ( $fFieldnames )
            {
                $sRow = $aText[ 0 ];
                if ( substr( $sRow, -1 ) == "\n" )
                    $sRow = substr( $sRow, 0, -1 );
                $aFieldnames = explode( ";", $sRow );
                while( list( $sKey, $sValue ) = each( $aFieldnames ) )
                {
                    // Cut left '"'
                    if ( substr( $sValue, 0, 1 ) == '"' )
                        $sValue = substr( $sValue, 1 );

                    // Cut right '"'
                    if ( substr( $sValue, -1 ) == '"' )
                        $sValue = substr( $sValue, 0, -1 );

                    // replace '""' with '"'
                    $sValue = str_replace( '""', '"', $sValue );

                    // Save new fieldname
                    $aFieldnames[ $sKey ] = $sValue;
                }
            }

            // Do something to all rows...
            while( list( $sKey, $sRow ) = each( $aText ) )
            {
                // Skip row with fieldnames
                if ( $fFieldnames && $sKey == 0 )
                    continue;

                if ( substr( $sRow, -1 ) == "\n" )
                    $sRow = substr( $sRow, 0, -1 );
                $aValues = explode( ";", $sRow );
                while( list( $sKey, $sValue ) = each( $aValues ) )
                {
                    // Cut left '"'
                    if ( substr( $sValue, 0, 1 ) == '"' )
                        $sValue = substr( $sValue, 1 );

                    // Cut right '"'
                    if ( substr( $sValue, -1 ) == '"' )
                        $sValue = substr( $sValue, 0, -1 );

                    // replace '""' with '"'
                    $sValue = str_replace( '""', '"', $sValue );

                    // Should we use fieldnames?
                    if ( $fFieldnames )
                        $sKey = $aFieldnames[ $sKey ];

                    // Save value in different array
                    $aRow[ $sKey ] = $sValue;
                }

                $aResult[] = $aRow;
            }
        }

        return( $aResult );
    }
}

}
// if ( !INCLUCDED_DBCSV )

?>