本文整理汇总了PHP中parseCSV::parse_string方法的典型用法代码示例。如果您正苦于以下问题:PHP parseCSV::parse_string方法的具体用法?PHP parseCSV::parse_string怎么用?PHP parseCSV::parse_string使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类parseCSV
的用法示例。
在下文中一共展示了parseCSV::parse_string方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: ImportCsv
function ImportCsv($file_lines, $delims = array(";", ",", "\t"), $quotes = array('"', "'"))
{
function maxChar($chars, $testString)
{
$max_count = 0;
$the_char = count($chars) > 0 ? $chars[0] : " ";
foreach ($chars as $char) {
$new_count = substr_count($testString, $char);
if ($new_count > $max_count) {
$max_count = $new_count;
$the_char = $char;
}
}
return $the_char;
}
$test_line = $file_lines[0];
//
// Detect the most probable delimiter.
//
$delim = maxChar($delims, $test_line);
$quote = maxChar($quotes, $test_line);
//
// Re-Conncat the file-lines
//
$input = implode("\n", $file_lines) . "\n";
//
// Setup and run the parser
//
include "lib/parsecsv.lib.php";
$csv = new parseCSV();
$csv->delimiter = $delim;
$csv->enclosure = $quote;
$csv->file_data =& $input;
$this->data = $csv->parse_string();
//
// Convert the array to addresses
//
$this->convertToAddresses();
}
示例2: parse_attendee_list
public function parse_attendee_list($csv_text)
{
$csv = new parseCSV();
$csv->delimiter = "\t";
$csv->data = $csv->parse_string($csv_text);
return $csv->data;
}
示例3: importEventsCSV
/**
* import events from CSV upload
* @param string $filepath
*/
protected function importEventsCSV($filepath)
{
global $wpdb;
$fp = fopen($filepath, 'r');
if ($fp === false) {
throw new EM_ImpExpImportException('error opening CSV file');
}
// read first line of CSV to make sure it's the correct format -- fgetscsv is fine for this simple task!
$header = fgetcsv($fp);
if ($header === false) {
throw new EM_ImpExpImportException('error reading import file or file is empty');
}
if (is_null($header)) {
throw new EM_ImpExpImportException('import file handle is null');
}
if (!is_array($header)) {
throw new EM_ImpExpImportException('import file did not scan as CSV');
}
if (!in_array('summary', $header)) {
throw new EM_ImpExpImportException('import file does not contain a field "summary"');
}
$wpdb->query('start transaction');
$records = 0;
$rows = 0;
$attrs = array();
$eventCategories = self::getEventCategories();
$eventCountries = self::getEventCountries();
$csv = new parseCSV();
$csv->fields = $header;
while ($line = fgets($fp)) {
$line = "\n{$line}\n";
// fix up line so that it can be parsed correctly
$cols = $csv->parse_string($line);
if ($cols) {
$rows++;
$cols = $cols[0];
// collect standard event properties
$data = array('uid' => isset($cols['uid']) ? trim($cols['uid']) : '', 'url' => isset($cols['url']) ? self::safeURL($cols['url']) : '', 'summary' => isset($cols['summary']) ? $cols['summary'] : '', 'dtstart' => isset($cols['dtstart']) ? $cols['dtstart'] : '', 'dtend' => isset($cols['dtend']) ? $cols['dtend'] : '', 'categories' => isset($cols['categories']) ? $cols['categories'] : '', 'freq' => isset($cols['freq']) ? $cols['freq'] : '', 'byday' => isset($cols['byday']) ? $cols['byday'] : '', 'interval' => isset($cols['interval']) ? $cols['interval'] : '', 'until' => isset($cols['until']) ? $cols['until'] : '', 'post_content' => isset($cols['post_content']) ? $cols['post_content'] : '', 'post_excerpt' => isset($cols['post_excerpt']) ? $cols['post_excerpt'] : '', 'event_spaces' => isset($cols['event_spaces']) ? $cols['event_spaces'] : '', 'location_name' => isset($cols['location_name']) ? $cols['location_name'] : '', 'location_address' => isset($cols['location_address']) ? $cols['location_address'] : '', 'location_town' => isset($cols['location_town']) ? $cols['location_town'] : '', 'location_state' => isset($cols['location_state']) ? $cols['location_state'] : '', 'location_postcode' => isset($cols['location_postcode']) ? $cols['location_postcode'] : '', 'location_country' => isset($cols['location_country']) ? $cols['location_country'] : '', 'location_region' => isset($cols['location_region']) ? $cols['location_region'] : '', 'location_latitude' => isset($cols['location_latitude']) ? $cols['location_latitude'] : '', 'location_longitude' => isset($cols['location_longitude']) ? $cols['location_longitude'] : '');
if (isset($eventCountries[strtolower($data['location_country'])])) {
$data['location_country'] = $eventCountries[strtolower($data['location_country'])];
}
// collect custom event attributes, being columns not found in standard event properties
$attrs = array();
foreach ($cols as $key => $value) {
if (strlen($value) > 0 && !isset($data[$key])) {
$attrs[$key] = $value;
}
}
// if we have location, try to either retrieve it by name, or create a new location object
$location = false;
if (self::hasLocation($data)) {
if ($data['location_name']) {
// try to find location by name
$location = $this->getLocationByName($data['location_name']);
}
if (!$location) {
// must create a new location object
$location = new EM_Location();
$location->location_name = empty($data['location_name']) ? self::fudgeLocationName($data) : $data['location_name'];
$location->location_address = empty($data['location_address']) ? $data['location_name'] : $data['location_address'];
$location->location_town = $data['location_town'];
$location->location_state = $data['location_state'];
$location->location_postcode = $data['location_postcode'];
$location->location_country = $data['location_country'];
$location->location_region = $data['location_region'];
$location->location_latitude = $data['location_latitude'];
$location->location_longitude = $data['location_longitude'];
self::maybeSetCoordinates($location);
$location->save();
}
}
// try to find existing event with matching unique ID first, so can update it
$event = false;
if ($data['uid']) {
add_filter('em_events_get_default_search', array(__CLASS__, 'filterEventArgs'), 10, 2);
add_filter('em_events_build_sql_conditions', array(__CLASS__, 'filterEventSQL'), 10, 2);
$event = EM_Events::get(array('em_impexp_uid' => $data['uid']));
$event = count($event) > 0 ? $event[0] : false;
remove_filter('em_events_get_default_search', array(__CLASS__, 'filterEventArgs'), 10, 2);
remove_filter('em_events_build_sql_conditions', array(__CLASS__, 'filterEventSQL'), 10, 2);
}
if (!$event) {
// must create a new event
$event = new EM_Event();
}
$event->location_id = $location ? $location->location_id : 0;
$event->event_attributes['em_impexp_uid'] = $data['uid'];
$event->event_attributes['em_impexp_url'] = $data['url'];
$event->event_name = $data['summary'];
$event->post_content = $data['post_content'];
$event->post_excerpt = $data['post_excerpt'];
if (preg_match('@^\\d\\d/\\d\\d/\\d\\d\\d\\d$@', $data['dtstart'])) {
$data['dtstart'] .= ' 00:00:00';
$event->start = date_create_from_format('d/m/Y H:i:s', $data['dtstart'])->getTimestamp();
$event->event_start_date = date('Y-m-d', $event->start);
$event->event_start_time = date('H:i:s', $event->start);
//.........这里部分代码省略.........
示例4: MeekroDB
}
$database = new MeekroDB(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME, DB_PORT, DB_CHARSET);
$database->throw_exception_on_error = true;
$database->error_handler = false;
$zipArchiveLocation = $fileName;
$zipContentsQueryable = getZipContentsAsQueryable($zipArchiveLocation);
$csvEntry = $zipContentsQueryable->where(function ($v) {
return endsWith($v["name"], "csv");
})->single();
$csvData = getZipContentOfFile($zipArchiveLocation, $csvEntry['name']);
while (ord($csvData[0]) > 127) {
$csvData = substr($csvData, 1);
}
$csvParser = new parseCSV();
$csvParser->delimiter = ";";
$csvContentsQueryable = from($csvParser->parse_string(utf8_decode($csvData)));
try {
$database->startTransaction();
$existingRecordsQueryable = from($database->query("SELECT * FROM dealer"));
$oldRecords = $existingRecordsQueryable->where('$v["IsDeleted"] == 0')->where(function ($row) use($csvContentsQueryable) {
return $csvContentsQueryable->all('$v["Reg No."] != ' . $row['RegistrationNumber']);
})->toArray();
Log::info(sprintf("Soft deleting old records on '%s'", 'dealer'));
from($oldRecords)->each(function ($row) use($database) {
Log::info(sprintf("Deleting %s", $row['RegistrationNumber']));
$database->update("dealer", array("LastChangeDateTimeUtc" => $database->sqleval("utc_timestamp()"), "IsDeleted" => 1), "Id=%s", $row["Id"]);
});
Log::info(sprintf("Inserting / updating records on '%s'", 'dealer'));
from($csvContentsQueryable)->each(function ($record) use($database, $existingRecordsQueryable, $zipArchiveLocation) {
$row = array("LastChangeDateTimeUtc" => $database->sqleval("utc_timestamp()"), "IsDeleted" => "0", "RegistrationNumber" => $record["Reg No."], "AttendeeNickname" => $record["Nick"], "DisplayName" => $record["Display Name"], "ShortDescription" => $record["Short Description"], "AboutTheArtistText" => $record["About the Artist"], "AboutTheArtText" => $record["About the Art"], "WebsiteUri" => $record["Website Reg"], "ArtPreviewCaption" => $record["Art Preview Caption"]);
$zipContentsQueryable = getZipContentsAsQueryable($zipArchiveLocation);