Skip to content

Instantly share code, notes, and snippets.

View vijinho's full-sized avatar

Vijay vijinho

  • England
View GitHub Profile
@vijinho
vijinho / cards.php
Created February 24, 2016 09:13
test payment card numbers
<?php
$cards = [
'MasterCard0' => '5555555555554444',
'MasterCard1' => '5105105105105100',
'Visa0' => '4111111111111111',
'Visa1' => '4012888888881881',
'Visa2' => '4929000005559',
'Visa3' => '4929000000014',
'Visa4' => '4929000000022',
'Visa5' => '4222222222222',
@vijinho
vijinho / download-csv.php
Last active February 22, 2016 15:12
Convert an array to a csv file
<?php
$filename = 'report_period_' . date('Y-m-d', $time) . '.csv';
$file = sys_get_temp_dir() . '/' . $filename;
$fp = fopen($file, 'w');
foreach ($csvData as $r) {
fputcsv($fp, $r);
}
fclose($fp);
$size_in_bytes = filesize($file);
<?php
// script to parse mac address db at
// https://code.wireshark.org/review/gitweb?p=wireshark.git;a=blob_plain;f=manuf
$data = file('vendors.txt');
foreach ($data as $line) {
if (preg_match("/^#.*/", $line)) {
continue;
} elseif (preg_match("/([A-F0-9]+:[A-F0-9]+:[A-F0-9]+)\s+[^#]+#\s+(.+)/i", $line, $matches)) {
@vijinho
vijinho / removeable_functions.php
Last active October 30, 2015 13:10
Find PHP legacy functions that are not in use in source code
<?php
/**
* Find unused functions in a legacy project that are safe to remove
*
* @author: Vijay Mahrra <[email protected]>
*/
$folder = 'legacy';
$cmd = "grep -aRi 'function ' $folder/*";
exec($cmd, $results);
$removeable = [];
@vijinho
vijinho / running.php
Last active October 1, 2015 13:18
PHP script already running? FreeBSD/Linux compatible
<?php
function running() {
// check if already running, quit if so
exec("ps auxww | grep running.php | grep -v grep", $ps);
if (count($ps) > 1) {
echo "Already running!\n";
return;
}
sleep(60);
}
@vijinho
vijinho / dump_mysql_db_table.sh
Created August 21, 2015 18:22
dump a mysql database table
#/usr/local/bin/bash
DB=$2
HOST=$1
TABLE=$5
USER=$3
PASS=$4
MYSQLDUMP=$(which mysqldump)
MYSQLDUMP_OPTS=" --no-create-db --add-locks --add-drop-table --extended-insert --verbose"
@vijinho
vijinho / dump_mysql_db.sh
Created August 21, 2015 18:20
dump a database with selective rows and tables
#!/bin/sh
MYSQLDUMP=`which mysqldump`
# mysql settings
DB="mysql"
HOST="127.0.0.1"
USER="root"
PASS="root"
@vijinho
vijinho / dump_mysql_db_schema.sh
Created August 21, 2015 18:17
dump mysql database schema
#!/bin/sh
MYSQLDUMP=`which mysqldump`
DB="mysql"
HOST="127.0.0.1"
USER="root"
PASS="root"
# backup file settings
@vijinho
vijinho / get_memory_used.php
Last active January 6, 2025 13:00
get memory used in php
<?php
/**
* Get memory usage in megabytes.
*
* @return string Memory usage in the format "used/peak" where both values are in MB.
*/
function get_memory_used(): string {
$memoryUsage = memory_get_usage() / 1024 / 1024;
$peakMemoryUsage = memory_get_peak_usage() / 1024 / 1024;
@vijinho
vijinho / drop_tables.sh
Created August 21, 2015 18:14
drop empty tables
#!/bin/sh
# path settings
MYSQL=`which mysql`
# mysql settings
DB="MYDB"
HOST="127.0.0.1"
USER="root"
PASS="root"