Skip to content

Instantly share code, notes, and snippets.

View DenesKellner's full-sized avatar
🍽️
There's a lot on my plate

Dénes Kellner DenesKellner

🍽️
There's a lot on my plate
View GitHub Profile
@DenesKellner
DenesKellner / run-my-php.bat
Last active October 13, 2020 11:03
A simple php file that's also a valid Windows batch; it will run itself, that is, whatever php is contained inside. This may be the simplest possible way to create command line php batches that look like real console commands.
@rem <?php /*
@d:\yada\yada\bin\php "%~dpnx0" %* & exit /b & rem */print "\r \r";
//--------------------------------------------------------------------------------------------------------------------
// Make sure you replace the "d:\yada\yada\bin\php" with your real php.exe path
//--------------------------------------------------------------------------------------------------------------------
print "This is a PHP script that looks like a batch.\n";
print "Anything that's below the line is normal PHP, you can write your script here.\n";
print "In fact, let's count to ten:\n";
for($c=0;$c++<10;) print "$c, ";
@DenesKellner
DenesKellner / randomNames.php
Created September 21, 2020 11:57
Generates N random person names. It has a built-in list for first and last names and it creates unique combinations, by a dumb retry-until-succeed algorithm which is fast enough for the job.
<?php
error_reporting(E_ALL - E_NOTICE);
$thatsTooMany = "That's too many.\nYou'll exhaust your memory.\n";
$desiredOutputCount = $argv[1] ?: 10; if($desiredOutputCount>500000) die($thatsTooMany);
$moreThanEnoughTries = $desiredOutputCount * 12;
$allFirstNames = wordsOf(firstNames);
$allLastNames = wordsOf(lastNames);
@DenesKellner
DenesKellner / scrollBeyond.js
Last active September 21, 2020 11:50
Allows you to easily apply styles based on current Y scroll value. It does this by defining a breakpoint (yes, a single one) and adding/removing a class as you cross that. Implemented with setInterval so it's resource-efficient and won't overwhelm your CPU when scrolling slowly. Has no dependencies, simple as hell.
setTimeout(function() {
var amount = 100; // how many pixels before the miracle happens
var beyondClass = 'beyond-that-point'; // this class will be added
var targetSelector = 'body'; // which element to add the class to
var checkMS = 20; // check scroll position every N milliseconds
var eClass = document.querySelector(targetSelector).classList;
setInterval(function() {
@DenesKellner
DenesKellner / argumentParser.php
Last active February 21, 2023 22:03
Command line argument parser. Very convenient, you can define your switches & parameters as if you were writing the help of your program. Handles string, integer and boolean types, supports numbered arguments (not counting the switches of course), and the same function can be used for declaration and parameter querying. No dependencies.
<?php
function arg($x="",$default=null) {
static $argtext = "";
static $arginfo = [];
/* helper */ $contains = function($h,$n) {return (false!==strpos($h,$n));};
/* helper */ $valuesOf = function($s) {return explode(",",$s);};
@DenesKellner
DenesKellner / wordSimilarity.php
Last active February 21, 2023 22:39
Text comparison by words, returning a similarity rate between 0..1 - 0 for nothing like it, 1 for exact match. Depending on how you use it, some prefiltering may be needed for punctuation & case & stuff.
<?php
function wordSimilarity($s1,$s2) {
$wordsof = function($s) {
$a=[];foreach(explode(" ",$s)as $w) if($w) $a[$w]=1;
return $a;
};
$w1 = $wordsof($s1); if(!$w1) return 0;
@DenesKellner
DenesKellner / deleteQuotedParts.php
Last active September 21, 2020 11:48
A string helper that replaces in-quote characters with a neutral one. It's good for handling things like SQL commands where quoted parts can contain keywords but you're not interested in them. Very simple tool, use with care.
<?php
function deleteQuotedParts($s,$replaceWith="#") {
$inQuote = "";
$protect = 0;
$sl = strlen($s);
$quoteChars = ["'"=>1,'"'=>1,"`"=>1];
for($i=0;$i<$sl;++$i) {
if($protect) {
@DenesKellner
DenesKellner / class.SimpleSheet.php
Last active September 21, 2020 11:49
Simple Excel XML spreadsheet generator that can convert your 2D array to a valid XML that you can open with Excel. It's based on https://en.wikipedia.org/wiki/Microsoft_Office_XML_formats - but adds a few perks like text width calculation.
<?php
class SimpleSheet {
private $data = [];
private $worksheetName = "Export";
private $widthMultiplier = 6; // how many points for one character; gives rough approximations
private $cellLimiter = " "; // add extra character after each cell value (overflow limiter)
private $rowLimiter = " "; // add extra cell at the end of each row (overflow limiter)
@DenesKellner
DenesKellner / speedTest.php
Last active September 12, 2021 22:51
A minimal benchmark framework that gives you high precision time measurements. The higher you set $outerLoops the more precise results you get.
<?php
'Options';{
$outerLoops = 1000; // number of times to repeat measure (choose shortest time), give it 1000+
$innerLoops = 30; // iterations within each measurement; depends on what you're measuring
$magnitude = 6; // one of [3,6,9], or [nanoseconds,microseconds,milliseconds], respectively
$decimals = 2; // normally 2 but be my guest
$methods = 3; // number (and order) of methods to run; simple integer or like [1,3,2]
@DenesKellner
DenesKellner / ajaxGet.js
Last active September 21, 2020 11:53
Simple AJAX call - thank you w3.js - that has no dependencies so you can live without jQuery and still make calls to the server.
function ajaxGet(url,cbStateChange,xmlData,method) {
var rq;
method = method||"GET";
try{
if(window.XMLHttpRequest)
{rq = new XMLHttpRequest();} else
{rq = new ActiveXObject("Microsoft.XMLHTTP");}
;;
}catch(e) {return;}
@DenesKellner
DenesKellner / showRecords.php
Last active September 21, 2020 11:54
Recordset viewer - featuring a fixed header that scrolls horizontally. It's good for any kind of 2D content where columns hold the same kind of information and you have more of them than you can fit in one screen.
<?php
function showRecords($list) {
$head = array_keys(reset($list));
?>
<style>
* {box-sizing:border-box;}
show-records { --header-height:40px; --header-color:steelblue; --fonts:calibri,arial,helvetica; }
show-records, scrolling-box, stable-header {display:block;}
show-records {box-shadow:0px 5px 14px -8px black;height:80vh;position:relative;}