Skip to content

Instantly share code, notes, and snippets.

@xexu
xexu / randomStringFromCharacterSet.php
Created February 17, 2012 18:06
Function to generate random strings of a given length containing characters from a given set
<?php
function randomStringFromCharacterSet($length, $possibleCharacters){
return substr(str_shuffle(str_repeat($possibleCharacters,$length)),0,$length);
}
//Example of usage
$possibleCharacters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
$length = 5;
for($i=0;$i<10;$i++){
echo randomStringFromCharacterSet($length, $possibleCharacters).'</br>';
@xexu
xexu / PHP Debug
Last active October 13, 2015 22:38
Just add this code on top of the file if the page goes blank to show errors
error_reporting(E_ALL);
ini_set("display_errors", "on");
@xexu
xexu / recursive list
Last active December 16, 2015 18:29
Rescursive list of files
ls -R /path | awk '
/:$/&&f{s=$0;f=0}
/:$/&&!f{sub(/:$/,"");s=$0;f=1;next}
NF&&f{ print s"/"$0 }'
@xexu
xexu / replacer.py
Created April 28, 2013 21:02
replace utility for large files
# -*- coding: utf-8 -*-
import os, codecs, sys
dir = os.getcwd()
file = sys.argv[1]
Dict = [['a', 'b'], ['c', 'd']]
fname = os.path.join(dir, file)
@xexu
xexu / dom.html
Last active December 17, 2015 20:49
DOM selection jQuery-ish on IE8+
<html>
<head>
<title>DOM selection jQuery-ish on IE8+</title>
</head>
<body>
<script>
window.$ = function(selector) {
return document.querySelector(selector);
};
@xexu
xexu / paypal-nvp.php
Created October 23, 2013 11:12
Script to query paypal nvp api
<?php
class Paypal {
/**
* Last error message(s)
* @var array
*/
protected $_errors = array();
/**
* API Credentials
@xexu
xexu / StartsWithEndsWith.php
Last active August 29, 2015 13:58
Handy StartsWith / EndsWith php functions
//http://stackoverflow.com/questions/834303/php-startswith-and-endswith-functions/834355#834355
//Note: Be careful with empty strings, you might want a different behaviour
function startsWith($haystack, $needle)
{
$length = strlen($needle);
return (substr($haystack, 0, $length) === $needle);
}
function endsWith($haystack, $needle)
<?php
backup_tables('','','','');
/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$database,$tables = '*')
{
$link = mysql_connect($host,$user,$pass);
mysql_select_db($database,$link);
@xexu
xexu / HTTPStatusChecker
Created October 2, 2014 16:30
Check http status of urls in a file
for i in `cat urls.txt`; do curl -s -o /dev/null -w "%{http_code} " $i; echo $i; done
@xexu
xexu / simple-naive-bayes-classifier.php
Created September 1, 2015 14:30
A PHP classifier in under 50 lines!
<?php
function tokenize($string)
{
$string = preg_replace("/ +/", " ", $string);
return explode(" ", strtolower($string));
}
function train($dataset, $text, $class)
{
$tokens=tokenize($text);