Skip to content

Instantly share code, notes, and snippets.

@mdurys
mdurys / is_base64.php
Created April 4, 2023 14:42
Check if given string is base64 encoded
private static function isBase64Encoded(string $text): bool
{
if (false === $decoded = base64_decode($text, true)) {
return false;
}
// check if decoded text consists only of unicode letters, digits and white space
if (!preg_match('|^[\p{L}\d\s]+$|u', $decoded)) {
return false;
}
@mdurys
mdurys / gist:e52c1a87310a065c98d7a9b9776002b7
Created January 24, 2019 17:19
Setting up Symfony project with Composer in Docker
docker run --rm --interactive --tty --volume $PWD:/app --user $(id -u):$(id -g) composer create-project symfony/skeleton my-api
cd my-api
docker run --rm --interactive --tty --volume $PWD:/app --user $(id -u):$(id -g) composer require --dev phpunit
@mdurys
mdurys / pdoattribute
Last active June 24, 2021 12:48
Doctrine2: setting PDO attribute on runtime
$em
->getConnection()
->getWrappedConnection()
->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
@mdurys
mdurys / circular right shift
Last active August 29, 2015 14:16
Do a circular right shift by $n bits on a 30-bit $value
$shifted = ($value >> $n) | ($value << (30 - $n) & 0x3fffffff);
@mdurys
mdurys / gist:ac64f9b0cc9441efeb1f
Created February 18, 2015 09:33
Rearrange lines in file in random order and split the result into files with given number of lines
sort --random-sort customer.csv | split --lines 29175
@mdurys
mdurys / gist:994fd6744f5969e08209
Created December 4, 2014 11:24
Identify Polish mobile phone numbers
SELECT COUNT(*)
FROM customer
WHERE is_active = 1
AND (
register_phone LIKE '50%'
OR register_phone LIKE '51%'
OR register_phone LIKE '53%'
OR register_phone LIKE '57%'
OR register_phone LIKE '60%'
OR register_phone LIKE '66%'
@mdurys
mdurys / gist:073044b9285cbc3d1c88
Created December 4, 2014 11:06
Format Unix timestamp in a MySQL query
SELECT
DATE_FORMAT(FROM_UNIXTIME(p.updated_at), '%Y-%m-%d') AS updated_at
FROM product p
@mdurys
mdurys / gist:0b0c67a26525ad127fad
Created December 4, 2014 10:54
Convert text file from UTF8 LF to CP1250 CRLF
iconv -f utf8 -t cp1250 file.txt | sed 's/$'"/`echo \\\r`/" >file_cp1250_crlf.txt