BernaLang
6/22/2018 - 9:48 AM

PHP useful

Some useful PHP code snippets

//Function to replace /r/n (chars that a line break gets replaced to when escape_string is executed)
function lnBreakRplc($str){
  return str_replace('\r\n', Chr(13).Chr(10), $str);
}
//Zip all files inside one folder
function zipFilesFolder($zipPath, $addZipPath = "", $folderGlob){
  if( empty($zipPath) || empty($folderGlob) ){
    return false;
  }
  $zip = new ZipArchive();
  $zipFileName = $zipPath;
  $ret = $zip->open( $zipFileName, ZipArchive::CREATE | ZipArchive::OVERWRITE);
  if ($ret !== TRUE) {
    return false;
  } else {
    $options = array('add_path' => $zippedFolder, 'remove_all_path' => TRUE);
    $zip->addGlob($folderGlob, GLOB_BRACE, $options);
    $zip->close();
    return true;
  }
}

//Example, zip all csv files in a folder, the zip will have the dir "exportedCSVs" -> (csv files)
zipFolderFiles("C:/zips/export.zip", "exportedCSVs", "C:/CSVs/*.csv");
function array2csv(array $array, $csvName, $folder){

  if (count($array) == 0) {
    return null;
  }
  
  $df = fopen($folder . $csvName, 'w');
  fputcsv($df, array_keys(reset($array)));
  foreach ($array as $row) {
    fputcsv($df, $row);
  }
  fclose($df);
  return $folder . $csvName;
}

foreach( sqlsrv_field_metadata( $stmt ) as $fieldMetadata ) {
  $headers[] = $fieldMetadata['Name'];
}

while( $obj = sqlsrv_fetch_array( $stmt )) {
  $tempx = array();
  foreach($headers as $hdr){
    $tempx[$hdr] = $obj[$hdr];
  }
  $array[] = $tempx;
}

array2csv($array, 'export.csv', 'C:\csv\');
//Convert a string into a "machine readable" string (ex: "Aãá A -> "aaa-a")
function slugify($text, $strict = false) {
    $text = html_entity_decode($text, ENT_QUOTES, 'UTF-8');
    // replace non letter or digits by -
    $text = preg_replace('~[^\\pL\d.]+~u', '-', $text);

    // trim
    $text = trim($text, '-');
    setlocale(LC_CTYPE, 'en_GB.utf8');
    // transliterate
    if (function_exists('iconv')) {
        $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
    }

    // lowercase
    $text = strtolower($text);
    // remove unwanted characters
    $text = preg_replace('~[^-\w.]+~', '', $text);
    if (empty($text)) {
        return 'empty_$';
    }
    if ($strict) {
        $text = str_replace(".", "_", $text);
    }
    return $text;
}
//Function to set the download headers
function download_send_headers($filename, $contLength) {
    // disable caching
    $now = gmdate("D, d M Y H:i:s");
    header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
    header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
    header("Last-Modified: {$now} GMT");

    // force download  
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");

    // disposition / encoding on response body
    header("Content-Disposition: attachment;filename={$filename}");
    header("Content-Transfer-Encoding: binary");
    header('Content-Length: ' . $contLength);
}