/ Gists

Gists

On gists

From http://programujte.com/forum/vlakno/30409-csfd-api/

PHP-PHPDOM

csfd-dom-xpath.php #

<?php

$dom = new domDocument;
$csfd = file_get_contents("http://www.csfd.cz/film/$csfd_id");
$html = (ord($csfd[0]) == 31) ? gzdecode($csfd) : $csfd;
@$dom->loadHTML($html);
$dom->preserveWhiteSpace = false;

$xpath = new DOMXPath($dom);
$nazvy = array();
$zeme = array();
$names_other = "";
$nodes = $xpath->query("//h1[@itemprop='name']");
$names_cs = $nodes->item(0)->nodeValue;

foreach($xpath->query("//ul[@class='names']/li/h3") as $li) {
    $nazvy[] = $li->nodeValue;
}
foreach($xpath->query("//ul[@class='names']/li/img") as $li) {
    $zeme[] = $li->getAttribute('alt');
}
for($i=0;$i<count($nazvy);$i++){
    if($i==count($nazvy)-1)
        $names_other .= $zeme[$i]."-".$nazvy[$i];
    else
        $names_other .= $zeme[$i]."-".$nazvy[$i].";";
}

$nodes = $xpath->query("//h2[@class='average']");
$hodnoceni = str_replace('%', '', $nodes->item(0)->nodeValue);

$nodes = $xpath->query("//p[@class='origin']");
$podrobnosti = explode(", ", $nodes->item(0)->nodeValue);

$nodes = $xpath->query("//p[@class='genre']");
$genre = str_replace(' / ', '@;@', $nodes->item(0)->nodeValue);

$nodes = $xpath->query("//span[@data-truncate='340']");
$hraji = $nodes->item(0)->nodeValue;

$nodes = $xpath->query("//div[@data-truncate='570']");
$popis = $nodes->item(0)->nodeValue;

$nodes = $xpath->query("//img[@class='film-poster']");
$poster_url = "http:".$nodes->item(0)->getAttribute('src');

On gists

Human Readable File Size with PHP

PHP Helpers-Filters-Plugins

gistfile1.php #

<?php
# http://jeffreysambells.com/2012/10/25/human-readable-filesize-php
function human_filesize($bytes, $decimals = 2) {
    $size = array('B','kB','MB','GB','TB','PB','EB','ZB','YB');
    $factor = floor((strlen($bytes) - 1) / 3);
    return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$size[$factor];
}

echo human_filesize(filesize('example.zip'));

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

Custom Router -- Nette

Nette

custom-router.php #

$router[] = new Route('<slug .+>', array(
    null => Route::FILTER_IN => function($params) {
        // nacist z databaze podle $params['slug'];
        if (nenalezeno) {
            return null;
        }
        unset($params['slug']);
        $params['module'] = '...';
        $params['presenter'] = '...';
        $params['action'] = '...';
        $params['id'] = '...';
        $params['...'] = '...';
        return $params;
    },
    null => Route::FILTER_OUT => function($params) {
        // nacist $slug z db prodle $params['module'], $params['presenter'], $params['action'], ...
        if (nenalezeno) {
            return null;
        }
        unset($params['module'], $params['presenter'], ...);
        $params['slug'] = $slug;
        return $params;
    },
));

On gists

Nahrazeni obrazku na webu 1px transparentnim gifem

JavaScript

replace-img.js #

(function(img) {
    for (var i = img.length; i--; ) {
        img[i].src = "data:image/gif;base64,R0lGODlhAQABAIAAAP///////yH5BAAHAP8ALAAAAAABAAEAAAICRAEAOw==";
    }
})(document.images);

On gists

České řazení / Czech sorting

PHP Helpers-Filters-Plugins

cs-sort.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

Recolor image From http://stackoverflow.com/questions/12178874/replace-a-color-with-another-in-an-image-with-php

PHP

recolor-image.php #

<?php
$imgname = "1.gif";
$im = imagecreatefromgif ($imgname);
$index = imagecolorexact ($im,0,128,0);
imagecolorset($im,$index,240,255,0);
$imgname = "result.gif";
imagegif($im,$imgname);
?>
<img src="result.gif">

On gists

From http://blog.bobbyallen.me/2015/04/04/get-nearest-places-from-mysql-with-latitude-and-longditude/

MySql

lat-lng.sql #

-- Set your users current location in LAT/LONG here
set @lat=51.891648;
set @lng=0.244799;

-- The main SQL query that returns the closest 5 airports.
SELECT id, icao, lat, lng, 111.045 * DEGREES(ACOS(COS(RADIANS(@lat))
 * COS(RADIANS(lat))
 * COS(RADIANS(lng) - RADIANS(@lng))
 + SIN(RADIANS(@lat))
 * SIN(RADIANS(lat))))
 AS distance_in_km
FROM airports
ORDER BY distance_in_km ASC
LIMIT 0,5;

On gists

Google Maps Simple Multiple Marker Example

Gmaps

Google Maps Simple Multiple Mark #

<html>
<head>
  
  <title>Google Maps Multiple Markers</title>
  <script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>
</head>
<body>
  <div id="map" style="height: 400px; width: 500px;">
</div>
<script type="text/javascript">
    var locations = [
      ['Bondi Beach', -33.890542, 151.274856, 4],
      ['Coogee Beach', -33.923036, 151.259052, 5],
      ['Cronulla Beach', -34.028249, 151.157507, 3],
      ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
      ['Maroubra Beach', -33.950198, 151.259302, 1]
    ];

    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 10,
      center: new google.maps.LatLng(-33.92, 151.25),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var infowindow = new google.maps.InfoWindow();

    var marker, i;

    for (i = 0; i < locations.length; i++) { 
      marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[i][1], locations[i][2]),
        map: map
      });

      google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
          infowindow.setContent(locations[i][0]);
          infowindow.open(map, marker);
        }
      })(marker, i));
    }
  </script>
</body>
</html>

On gists

Povolení načtení webu ve Frames, Nette

Nette Nette-Neon Nette-Tricks

example.neon #

common:
	security:
		debugger: true
		frames: SAMEORIGIN