note 105658 deleted from function.str-getcsv by danbrown
| From: | danbrown@php.net | Date: | Tue, 06 Sep 2011 15:34:36 +0000 |
| Subject: | note 105658 deleted from function.str-getcsv by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-182977@lists.php.net to get a copy of this message | ||
Note Submitter: webmaster at mp3s dot pl
----
If you want to parse tsv (or any other string separated by some definied characters) you could use
that function i made:
<?
function parse_tsv($string=false, $separatot="\t") {
if($string) {
$linebyline = preg_split("|\r\n|is",$string);
foreach($linebyline as $key => $eachline) {
if(trim($eachline)) {
$column = preg_split("|".$separatot."|is",$eachline);
//Przypisanie naglowkow i wartoÅci
foreach($column as $columnkey => $eachvalue) {
if($key==0) {
$headers[] = $eachvalue;
} else {
$values[$key]["$headers[$columnkey]"] = $eachvalue;
}
}
}
}
$sorted_array = array_values($values);
return $sorted_array;
} else {
return false;
}
}
You can enter string with column names like:
$string = "Name Surname
Jan Kowalski
Damian Nowak";
print_r(parse_tsv($string));
And it will return:
Array
(
[0] => Array
(
[Name] => Jan
[Surname] => Kowalski
)
[1] => Array
(
[Name] => Damian
[Surname] => Nowak
)
)
?>