Skip to content

Instantly share code, notes, and snippets.

@mikkohei13
mikkohei13 / isinvalid.php
Created October 24, 2012 07:52
Regular expression validator: returns TRUE if subject does not fully match pattern
function isInvalid($subject, $pattern)
{
preg_match($pattern, $subject, $matches);
if ($matches[0] == $subject)
{
return FALSE;
}
else
{
return TRUE;
@mikkohei13
mikkohei13 / Converts XML to array
Last active October 12, 2015 02:58
XML to Array
// Source: http://www.php.net/manual/en/book.simplexml.php#108035
function toArray(SimpleXMLElement $xml) {
$array = (array)$xml;
foreach ( array_slice($array, 0) as $key => $value ) {
if ( $value instanceof SimpleXMLElement ) {
$array[$key] = empty($value) ? NULL : toArray($value);
}
}
@mikkohei13
mikkohei13 / checkUTF8BOM.php
Created November 27, 2012 11:09
Checks if file is UTF-8 and without BOM
/*
Thanks to
http://www.php.net/manual/en/function.mb-detect-encoding.php#91051
http://stackoverflow.com/questions/8907196/check-if-csv-file-is-in-utf-8-with-php
*/
if (! mb_check_encoding($data, 'UTF-8')) {
exit("Not UTF-8");
}
else
/**
* Gets the last commit date from .git repository
* @param string $gitLocation location of .git/logs/HEAD
* @return string date of last commit YYYY-mm-dd
*/
function gitLastCommitInfo($gitLocation)
{
$commitArray = file($gitLocation); // All commits from file
$lastCommit = array_pop($commitArray); // Latest commit
$lastCommitArray = explode("\t", $lastCommit);
/**
* Return base url automatically for Codeigniter
* Source: http://snipplr.com/view/16894/codeigniter-automatic-base-url-configuration/
*/
$config['base_url'] = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https" : "http");
$config['base_url'] .= "://".$_SERVER['HTTP_HOST'];
$config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
<?php
// Getting species/taxon images from EOL
$species = $_GET['species'];
$species = str_replace(" ", "+", $species);
// Id
// Documentation: http://eol.org/api/docs/pages
$idUrl = "http://eol.org/api/search/1.0.json?q=" . $species . "&page=1&exact=false&filter_by_taxon_concept_id=&filter_by_hierarchy_entry_id=&filter_by_string=&cache_ttl=";
// Regex for matching strings that do not contain comma:
// BAsed on: http://stackoverflow.com/questions/717644/regular-expression-that-doesnt-contain-certain-string
^((?!,).)*$
@mikkohei13
mikkohei13 / talvilintureitit.php
Created December 20, 2015 22:40
Työkalu html-taulukon luomiseen talvilintulaskentareittiluettelosta
<?php
header('Content-Type: text/html; charset=utf-8');
?>
<style>
.number
{
text-align: right;
}
</style>
<pre>
@mikkohei13
mikkohei13 / rainRadarExtract.py
Created October 16, 2020 21:31
Python code to extract rain intensity colors from FMI rain radar maps.
from PIL import Image
import colorsys
def rgb2hsv(rgb):
r = rgb[0] / 255
g = rgb[1] / 255
b = rgb[2] / 255
hsvRaw = colorsys.rgb_to_hsv(r, g, b)
@mikkohei13
mikkohei13 / capture.py
Created April 10, 2021 18:57
Capture stills from Youtube video
import pafy
import cv2
import math
from datetime import datetime
url = "add url here"
video = pafy.new(url)
best = video.getbest(preftype="mp4")