/ Gists / PHP

Gists - PHP

On gists

czech sorting

PHP

czech sorting.php #

<?php

setlocale(LC_ALL, 'cs_CZ.UTF-8');
header("content-type: text/html; charset=UTF-8");

$arr = explode(",", "č, d, b, a, š, ř, o, x, z, ž, á, s, m");

uasort($arr, "strcoll");
echo "<pre>" . print_r($arr, 1) . "</pre>";

On gists

Komplení regexp na username

PHP

User RegExp #

$userRegExp = "#^[_A-Za-z0-9áäéëěíóöôúůüýčďňŕřšťžĺľÁÄÉËĚÍÓÖÔÚŮÜÝČĎŇŔŘŠŤŽĹĽ ]{3,40}\$#";

On gists

PHP Curl to check if url is alive

PHP

curl_example.php #

<?php

function check_alive($url, $timeout = 10, $successOn = array(200, 301)) {
  $ch = curl_init($url);

  // Set request options
  curl_setopt_array($ch, array(
    CURLOPT_FOLLOWLOCATION => false,
    CURLOPT_NOBODY => true,
    CURLOPT_TIMEOUT => $timeout,
    CURLOPT_USERAGENT => "page-check/1.0"
  ));

  // Execute request
  curl_exec($ch);

  // Check if an error occurred
  if(curl_errno($ch)) {
    curl_close($ch);
    return false;
  }

  // Get HTTP response code
  $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  curl_close($ch);

  // Page is alive if 200 OK is received
  //return $code;
  return in_array( $code, $successOn );
}

$checks = array(
	'http://www.google.co.uk',
	'http://www.facebook.com',
	'http://www.bbc.co.uk',
	'http://photogabble.co.uk',
	'http://youtube.com'
	);

foreach($checks as $check)
{
	echo $check . ' is ' . ( (check_alive($check) ) ? 'Alive' : 'Dead' ) . "\n";
}

On gists

Download via Iframe // wo refresh

PHP

Downlad file via iframe (no refr #

<a href="" id="csv-export">export</a>
<iframe id="downloadIframe" src="" style="height: 0px; width: 0px; display: none;"></iframe>
<script>
    $("#csv-export").click(function(e){
        e.preventDefault();
        oIFrm = document.getElementById('downloadIframe');
        oIFrm.src = '' // URL to download file -- via php download headers;
    });
</script>

On gists

Convert UTF - win1250 - CSV

PHP

Convert utf-8 to win1250 - csv e #

 <?php
 
 $out = iconv('utf-8', 'windows-1250//TRANSLIT', $out);
 
         
 $filename = 'katalog-export___' . date('YmdHis') . '.csv';
 header('Content-Encoding: windows-1250');
 header('Content-type: text/csv; charset=windows-1250');
 header('Content-Disposition: attachment; filename=' . $filename);
 //echo "\xEF\xBB\xBF"; // UTF-8 BOM
 echo $out;
 exit(0);
 
 ?>

On gists

Kontrola BOOMu na začátku souboru

PHP

UTF-8 BOOM check #

echo "\xEF\xBB\xBF"; // UTF-8 BOM

On gists

Validation date & time

PHP

PHP: Date validation (datetime) #

function validateDate($date, $format = 'Y-m-d H:i:s')
{
    $d = DateTime::createFromFormat($format, $date);
    return $d && $d->format($format) == $date;
}

var_dump(validateDate('2012-02-28 12:12:12')); # true
var_dump(validateDate('2012-02-30 12:12:12')); # false
var_dump(validateDate('2012-02-28', 'Y-m-d')); # true
var_dump(validateDate('28/02/2012', 'd/m/Y')); # true
var_dump(validateDate('30/02/2012', 'd/m/Y')); # false
var_dump(validateDate('14:50', 'H:i')); # true
var_dump(validateDate('14:77', 'H:i')); # false
var_dump(validateDate(14, 'H')); # true
var_dump(validateDate('14', 'H')); # true

var_dump(validateDate('2012-02-28T12:12:12+02:00', 'Y-m-d\TH:i:sP')); # true
# or
var_dump(validateDate('2012-02-28T12:12:12+02:00', DateTime::ATOM)); # true

var_dump(validateDate('Tue, 28 Feb 2012 12:12:12 +0200', 'D, d M Y H:i:s O')); # true
# or
var_dump(validateDate('Tue, 28 Feb 2012 12:12:12 +0200', DateTime::RSS)); # true
var_dump(validateDate('Tue, 27 Feb 2012 12:12:12 +0200', DateTime::RSS)); # false
# ...

On gists

Date range - vygenerování datum. rozmezí

PHP

array of date range.php #

<?php 

function dateRange( $first, $last, $step = '+1 day', $format = 'Y/m/d' ) {

	$dates = array();
	$current = strtotime( $first );
	$last = strtotime( $last );

	while( $current <= $last ) {

		$dates[] = date( $format, $current );
		$current = strtotime( $step, $current );
	}

	return $dates;
}

?>

On gists

PHP: CSV export from MySql to output

PHP

csv-export.php #

<?
    $out = '';
    $fields = dibi::fetchAll("SHOW COLUMNS FROM web_application");
    foreach ($fields as $field)
    {
    $out .= '"'.$field->Field.'";';
    }
    $out = rtrim($out, ';');
    $out .= PHP_EOL;
    foreach ($this->model->csvExport() as $index => $row)
    {
    foreach ($row as $r)
    {
    $out .='"'.$r.'";';
    }
    $out = rtrim($out, ';');
    $out .= PHP_EOL;
    }
    $out = iconv('utf-8', 'windows-1250', $out);
    $filename = 'application-export___' . date('YmdHis') . '.csv';
    header('Content-Encoding: windows-1250');
    header('Content-type: text/csv; charset=windows-1250');
    header('Content-Disposition: attachment; filename=' . $filename);
    //echo "\xEF\xBB\xBF"; // UTF-8 BOM
    echo $out;
    exit(0);
    $this->view->setTemplateFile('modules/application/csv.php');

On gists

PHP: Readable filesize

PHP Helpers-Filters-Plugins

readable-filesize.php #

<? 

function HumanReadableFilesize($size) {
// Adapted from: http://www.php.net/manual/en/function.filesize.php
$mod = 1024;
$units = explode(' ','B KB MB GB TB PB');
for ($i = 0; $size > $mod; $i++) {
$size /= $mod;
}
return round($size, 2) . ' ' . $units[$i];
}

function fsize($file) {
$a = array("B", "KB", "MB", "GB", "TB", "PB");
$pos = 0;
$size = filesize($file);
while ($size >= 1024) {$size /= 1024;$pos++;}
return round($size,2)." ".$a[$pos];
}

?>