Skip to content

Instantly share code, notes, and snippets.

@wesleybliss
wesleybliss / js_word_frequency.js
Created September 29, 2012 20:00
JavaScript Word Frequency + Some Options
/**
* Find how often words appear in a string, optionally
* limiting to ones that occur with a given frequency.
*
* @param {String} searchString The sentence to search on.
* @param {Integer} frequency The frequency at which you want to find words with.
* @param {Boolean} caseSensitive Whether to use case sensitive searching.
* @param {Booolean} stripPunctuation If punctuation should be removed before parsing.
*/
var wordFrequency = function( searchString, frequency, caseSensitive, stripPunctuation ) {
@wesleybliss
wesleybliss / translate_unc_delimiters.php
Created September 29, 2012 20:24
Translate UNC Delimiters
/**
* Translate a path to match host OS
*
* @param string $path Path or URI to be translated
* @return string Translated path
*/
function translate_UNC_delimiters( $path ) {
$path = str_replace( '\\', DIRECTORY_SEPARATOR, $path );
return str_replace( '/', DIRECTORY_SEPARATOR, $path );
}
@wesleybliss
wesleybliss / wp_get_page_id_from_slug.php
Created September 29, 2012 20:24
WordPress: Get Page ID From Slug
/**
* Get a page's ID from it's slug
*
* @param string $slug Page slug to search on
* @return null Empty on error
* @return int Page ID
*/
function get_page_ID_by_slug( $slug ) {
$page = get_page_by_path( $slug );
if ( $page ) {
@wesleybliss
wesleybliss / gist:3805136
Created September 29, 2012 20:40
File Name & Extension Parsing
// Get a file's extension
$ext = array_pop( explode( '.', $fileName ) );
// Get a file's name without extension
$name = array_shift( explode( '.', $fileName ) );
@wesleybliss
wesleybliss / gist:3813778
Created October 1, 2012 19:11
PHP Check If Parentheses Are Balanced
<?php
// Determine if there is an equal number of parentheses
// and if they balance logically, i.e.
// ()()) = Bad (trailing ")")
// (())()() = GOOD
// )()()(()) = BAD (leading ")")
function is_balanced( $s ) {
// Keep track of number of open parens
@wesleybliss
wesleybliss / gist:3825115
Created October 3, 2012 05:04
Basic PHP Class Example
<?php
// Only allow this script to be run via the command line
if ( strtoupper(PHP_SAPI) !== 'CLI' ) {
header( 'Content-type: text/plain' );
echo 'This script can only be run via the command line.',
PHP_EOL, 'Usage: php -f ',
array_pop(explode('/', $_SERVER['SCRIPT_NAME']));
exit( 1 );
}
@wesleybliss
wesleybliss / gist:4041103
Created November 8, 2012 19:52
WordPress Update Site URL
SELECT option_value
FROM wp_options
WHERE option_name = "home"
OR option_name = "siteurl"
LIMIT 1;
UPDATE wp_options
SET option_value = "http://newlink"
WHERE option_name IN ("home", "siteurl");
@wesleybliss
wesleybliss / print_r.js
Created March 22, 2013 16:25
Basic JavaScript print_r() method (minified).
var print_r=function(x,max,sep,l){l=l||0;max=max||10;sep=sep||' ';if(l>max){return"[WARNING: Too much recursion]\n"}var i,r='',t=typeof x,tab='';if(x===null){r+="(null)\n"}else if(t=='object'){l++;for(i=0;i<l;i++){tab+=sep}if(x&&x.length){t='array'}r+='('+t+") :\n";for(i in x){try{r+=tab+'['+i+'] : '+print_r(x[i],max,sep,(l+1))}catch(e){return"[ERROR: "+e+"]\n"}}}else{if(t=='string'){if(x==''){x='(empty)'}}r+='('+t+') '+x+"\n"}return r};var_dump=print_r;
@wesleybliss
wesleybliss / cli_starter.php
Created August 15, 2013 21:33
PHP CLI script starter - useful for quickly setting up command-line scripts.
<?php
// Don't allow PHP to stop after global timeout setting
set_time_limit( 0 );
// Increase memory limit
ini_set( 'memory_limit', '1024M' );
// Remove output buffering
while ( ob_get_level() ) ob_end_clean();
@wesleybliss
wesleybliss / gist:6719458
Created September 26, 2013 19:40
Prepend smile/frown face to command prompt based on command execution result.
PS1="\w \`if [ \$? = 0 ]; then echo -e '\[\e[01;32m\]\n^_^'; else echo -e '\[\e[01;31m\]\n>_<'; fi\` \[\e]0;\w\a\] \[\e[32m\]\u@\h \[\e[33m\]\w\[\e[0m\]\\$\n"