note 86713 modified in function.substr by danbrown

From: Date: Fri, 28 Nov 2008 20:12:25 +0000
Subject: note 86713 modified in function.substr by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-147187@lists.php.net to get a copy of this message
<?php $cfg[csvEnc] = '"'; $cfg[csvEsc] = '\\'; $cfg[csvTerm] = ","; if( !function_exists("parse_csv_aux") ){ function parse_csv_aux( $string ){ global $cfg; $product = ""; $in_quote = FALSE; $skipped_quote = FALSE; for($i = 0 ; $i < strlen($string) ; $i++){ if( $string{$i} == $cfg[csvEnc] ){ if($in_quote){ if($skipped_quote){ $product .= $cfg[csvEnc]; $skipped_quote = FALSE; } else if( !$skipped_quote ){ $skipped_quote = TRUE; } $in_quote = FALSE; } else{ if($skipped_quote) $skipped_quote = FALSE; $in_quote = TRUE; } } else if( $string{$i} == "," ){ if($in_quote){ $product .= ","; } else{ $product .= " ~ "; } } else{ if($in_quote){ //$in_quote = FALSE; $product .= $string{$i}; } else{ $product .= $string{$i}; } } } return $product; } } if( !function_exists("parse_csv") ){ function parse_csv($string){ global $cfg; $data = array(); if( is_string($string) && ( stripos($string, "\n") !== FALSE ) ){ $data = explode("\n", parse_csv_aux($string) ); foreach($data as $key => $row){ $columns = array(); //$row = strtr( $row, array( "\";\"" => "\";\"", ";" => " ; " ) ); if( stripos($row, " ~ ") !== FALSE ){ $columns = explode( " ~ ", $row ); if( !is_array($columns) )$columns = array( strval($columns) ); $data[$key] = $columns; } } return $data; } else if( is_string($string) && ( stripos( ($string = parse_csv_aux($string)), " ~ ") !== FALSE ) ){ $columns = explode( " ~ ", $string ); if( !is_array($columns) )$columns = array( strval($columns) ); return array($columns); } else return strval($string); } /* end function parse_csv */ } /* end not function exists parse_csv */ if( !function_exists("store_csv_aux") ){ function store_csv_aux( $string ){ global $cfg; $string = strtr( $string, array( "\n" => "" ) ); $product = ""; $in_quote = FALSE; for( $i = 0 ; $i < strlen($string) ; $i++ ){ if( $string{$i} == $cfg[csvEnc] ){ if($in_quote){ $product .= "\"{$cfg[csvEnc]}"; } else{ $product .= "\"\"{$cfg[csvEnc]}"; $in_quote = TRUE; } } else if( $string{$i} == "," ){ if($in_quote){ $product .= ","; } else{ $product .= "\","; $in_quote = TRUE; } } else{ if($in_quote){ $product .= $cfg[csvEnc]; $in_quote = FALSE; $product .= $string{$i}; } else{ $product .= $string{$i}; } } } if($in_quote)$product .= $cfg[csvEnc]; return $product; } } if( !function_exists("store_csv") ){ function store_csv($data){ global $cfg; if(!is_array($data))return strval($data); $passed_rows = FALSE; $product = ""; foreach($data as $row){ if( $passed_rows )$product .= "\n"; if( is_array($row) ){ $columns = ""; $passed_cols = FALSE; foreach($row as $column){ if( $passed_cols )$columns .= ","; $columns .= store_csv_aux( $column ); $passed_cols =TRUE; } $product .= strval($columns); } else{ $product .= strtr( strval($row), array("\n" => "") ); } $passed_rows = TRUE; } return $product; } /* end function store_csv */ } /* end not function exists store_csv */ ?> [EDIT BY danbrown AT php DOT net: This is a bugfix rewrite of a function originally written by "Alexander Peev".] --was-- Fix the Alexander Peev's parsing functions. <?php $cfg[csvEnc] = '"'; $cfg[csvEsc] = '\\'; $cfg[csvTerm] = ","; if( !function_exists("parse_csv_aux") ){ function parse_csv_aux( $string ){ global $cfg; $product = ""; $in_quote = FALSE; $skipped_quote = FALSE; for($i = 0 ; $i < strlen($string) ; $i++){ if( $string{$i} == $cfg[csvEnc] ){ if($in_quote){ if($skipped_quote){ $product .= $cfg[csvEnc]; $skipped_quote = FALSE; } else if( !$skipped_quote ){ $skipped_quote = TRUE; } $in_quote = FALSE; } else{ if($skipped_quote) $skipped_quote = FALSE; $in_quote = TRUE; } } else if( $string{$i} == "," ){ if($in_quote){ $product .= ","; } else{ $product .= " ~ "; } } else{ if($in_quote){ //$in_quote = FALSE; $product .= $string{$i}; } else{ $product .= $string{$i}; } } } return $product; } } if( !function_exists("parse_csv") ){ function parse_csv($string){ global $cfg; $data = array(); if( is_string($string) && ( stripos($string, "\n") !== FALSE ) ){ $data = explode("\n", parse_csv_aux($string) ); foreach($data as $key => $row){ $columns = array(); //$row = strtr( $row, array( "\";\"" => "\";\"", ";" => " ; " ) ); if( stripos($row, " ~ ") !== FALSE ){ $columns = explode( " ~ ", $row ); if( !is_array($columns) )$columns = array( strval($columns) ); $data[$key] = $columns; } } return $data; } else if( is_string($string) && ( stripos( ($string = parse_csv_aux($string)), " ~ ") !== FALSE ) ){ $columns = explode( " ~ ", $string ); if( !is_array($columns) )$columns = array( strval($columns) ); return array($columns); } else return strval($string); } /* end function parse_csv */ } /* end not function exists parse_csv */ if( !function_exists("store_csv_aux") ){ function store_csv_aux( $string ){ global $cfg; $string = strtr( $string, array( "\n" => "" ) ); $product = ""; $in_quote = FALSE; for( $i = 0 ; $i < strlen($string) ; $i++ ){ if( $string{$i} == $cfg[csvEnc] ){ if($in_quote){ $product .= "\"{$cfg[csvEnc]}"; } else{ $product .= "\"\"{$cfg[csvEnc]}"; $in_quote = TRUE; } } else if( $string{$i} == "," ){ if($in_quote){ $product .= ","; } else{ $product .= "\","; $in_quote = TRUE; } } else{ if($in_quote){ $product .= $cfg[csvEnc]; $in_quote = FALSE; $product .= $string{$i}; } else{ $product .= $string{$i}; } } } if($in_quote)$product .= $cfg[csvEnc]; return $product; } } if( !function_exists("store_csv") ){ function store_csv($data){ global $cfg; if(!is_array($data))return strval($data); $passed_rows = FALSE; $product = ""; foreach($data as $row){ if( $passed_rows )$product .= "\n"; if( is_array($row) ){ $columns = ""; $passed_cols = FALSE; foreach($row as $column){ if( $passed_cols )$columns .= ","; $columns .= store_csv_aux( $column ); $passed_cols =TRUE; } $product .= strval($columns); } else{ $product .= strtr( strval($row), array("\n" => "") ); } $passed_rows = TRUE; } return $product; } /* end function store_csv */ } /* end not function exists store_csv */ ?> http://php.net/manual/en/function.substr.php

« previous php.notes (#147187) next »