Replace Funky Characters While Importing CSV

Sometimes uploaded text/csv file may have non-utf8 or other funky characters using the function below.

public static function processUploadedBundles($request)
	{
		$content = file_get_contents($request->file('uploadedFile')->getRealPath());

        $lines = explode(PHP_EOL, $content);
        $array = [];
        foreach ($lines as $line) {
            $arrayCsv = str_getcsv($line, ",");
            $arrayCsv = array_map(function($value){
                return preg_replace('/[\x00-\x1F\x7F-\xFF]/', '', $value);
            }, $arrayCsv);
            $array[] = $arrayCsv;
		}
		
		return $array;
	}
Share this article

Similar Posts

  • API Client Design

    When you extensively work with certain APIs, like Shopify’s for example, you will end up with bunch of functions that map to API’s endpoints. One of the approaches I have seen so far is to create an API class ShopifyApi and make those functions class methods. So it looks something like the figure below. I…

    Share this article
  • Upload to FTP with PHP

    $fp = fopen(‘https://www.example.com/pdfdoc’, ‘r’); $user = “sammy”; $pass = “password”; $ftp_server = “192.168.10.10”; //should be wrapped in try catch to properly handle errors $ftp_conn = ftp_ssl_connect($ftp_server); $login = ftp_login($ftp_conn, $user, $pass); ftp_chdir($ftp_conn, ‘path/to/folder’); //can also use ftp_pwd ftp_pasv($ftp_conn, true); //passive mode ftp_fput($ftp_conn, “mydocument.pdf”, $fp, FTP_BINARY); fclose($fp); ftp_close($ftp_conn); Above code can be used to upload a…

    Share this article
  • | |

    Laravel Jenkins CI

    This article covers installation of Jenkins on Ubuntu server and its usage to continuously integrate a Laravel application.  Besides LAMP/LEMP stack we need to install Java, Git, Composer, and Node to successfully use Jenkins. Before starting to install this software, let’s take care of miscellaneous  stuff. Miscellaneous (can skip this). Create mysql user and database….

    Share this article
  • |

    Debugging Webhooks

    A webhook is an HTTP callback, that occurs when something happens (a resource changes its state). Webhooks provide a way to build event-driven apps, because you can be notified about changes. Since webhooks require a publicly accessible URL to function they can be hard to test from your local machine.  There are three main problems…

    Share this article
  • |

    Composer: Path Repositories

    When working on a php package it is inconvenient to push the package to github (or other repository) and then wait for the package to update using composer update.  For package development, composer has such feature as path repositories.  Let’s imagine we have a two folders on the same level:  my-app and  package.   my-app is an app — a test…

    Share this article