Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
This is my general functions file. Lots of them aren't mine - I've indicated where I've found them where appropriate
<?php
/**
* Rich Text cleaner
*
* This function pulls out rich text quotes and dashes, usually from Word or other rich text editors
*
* @author Mango
* @link http://www.toao.net/48-replacing-smart-quotes-and-em-dashes-in-mysql
* @param string $text text to clean up
* @return string
**/
function richClean($text) {
$text = str_replace(
array("\xe2\x80\x98", "\xe2\x80\x99", "\xe2\x80\x9c", "\xe2\x80\x9d", "\xe2\x80\x93", "\xe2\x80\x94", "\xe2\x80\xa6"),
array("'", "'", '"', '"', '-', '--', '...'),
$text);
// Next, replace their Windows-1252 equivalents.
$text = str_replace(
array(chr(145), chr(146), chr(147), chr(148), chr(150), chr(151), chr(133)),
array("'", "'", '"', '"', '-', '--', '...'),
$text);
return($text);
}
/**
* Show the results of a print_r without having to view source
*
* @author gadhra
* @param array $arr the array to print out
* @param boolean $exit whether the function should exit after you print the array. Defaults to true
**/
if(! function_exists('debug')) {
function debug($arr, $exit=true) {
echo '<pre>';
print_r($arr);
echo '</pre>';
if($exit) {
exit(0);
}
}
}
/**
* Serialize and unserialize data for storage. This gets around the magic_quotes issue
* by encoding the results - helps when you're saving it to the db
*
* @author gadhra
* @param string $str base64 encoded serialized string
* @param array $arr base64 decoded unserialized array
* @return string|array depending on which function
**/
function srl($arr) {
if(is_array($arr)) {
return(base64_encode(serialize($arr)));
}
return(false);
}
function unsrl($str) {
$arr = @unserialize(base64_decode($str));
if(is_array($arr) ) {
return($arr);
}
return(false);
}
/**
* Wrapper for server side redirection
*
* @author gadhra
* @param string $url URL to redirect to
* @param string $type header type to send
**/
function go($url,$type='301' ) {
$h_types = arr_headers();
header("HTTP/1.1 $type $h_types[$type]");
header("Location:$url");
exit(0);
}
/**
* When I was moving accounts from MT to WP, I found that the
* old MT binaries were encrypting passwords using CRYPT(), while
* WP was using MD5. This was my workaround - worked about 80% of the
* time. Note: this is real specific to WP, but might have some use to s
* someone. Hooked from the pluggable.php file
*
* @author gadhra
* @param string $password Plaintext user's password
* @param string $hash Hash of the user's password to check against.
* @return bool False, if the $password does not match the hashed password
**/
function check_crypt_pass($check, $password, $hash, $user_id = '') {
if($check) return true;
$user_info = get_userdata($user_id);
$salt='';
for($i=0;$i<2;$i++) {
$salt .= $hash[$i];
}
$check= ($hash == crypt($password,$salt) );
if ( $check && $user_id ) {
// Rehash using new hash.
wp_set_password($password, $user_id);
$hash = wp_hash_password($password);
return true;
}
return false;
}
/**
* These are all functions that just return arrays I got tired of writing
*
* @author gadhra
**/
function arr_states( ) {
return array(
'AL'=>'Alabama',
'AK'=>'Alaska',
'AZ'=>'Arizona',
'AR'=>'Arkansas',
'CA'=>'California',
'CO'=>'Colorado',
'CT'=>'Connecticut',
'DE'=>'Delaware',
'DC'=>'District of Columbia',
'FL'=>'Florida',
'GA'=>'Georgia',
'GU'=>'Guam',
'HI'=>'Hawaii',
'ID'=>'Idaho',
'IL'=>'Illinois',
'IN'=>'Indiana',
'IA'=>'Iowa',
'KS'=>'Kansas',
'KY'=>'Kentucky',
'LA'=>'Louisiana',
'ME'=>'Maine',
'MD'=>'Maryland',
'MA'=>'Massachusetts',
'MI'=>'Michigan',
'MN'=>'Minnesota',
'MS'=>'Mississippi',
'MO'=>'Missouri',
'MT'=>'Montana',
'NE'=>'Nebraska',
'NV'=>'Nevada',
'NH'=>'New Hampshire',
'NJ'=>'New Jersey',
'NM'=>'New Mexico',
'NY'=>'New York',
'NC'=>'North Carolina',
'ND'=>'North Dakota',
'OH'=>'Ohio',
'OK'=>'Oklahoma',
'OR'=>'Oregon',
'PA'=>'Pennsylvania',
'PR'=>'Puerto Rico',
'RI'=>'Rhode Island',
'SC'=>'South Carolina',
'SD'=>'South Dakota',
'TN'=>'Tennessee',
'TX'=>'Texas',
'UT'=>'Utah',
'VT'=>'Vermont',
'VA'=>'Virginia',
'VI'=>'Virgin Islands',
'WA'=>'Washington',
'WV'=>'West Virginia',
'WI'=>'Wisconsin',
'WY'=>'Wyoming'
);
}
function arr_headers() {
return array(
'100'=>'Continue',
'101'=>'Switching Protocols',
'200'=>'OK',
'201'=>'Created',
'202'=>'Accepted',
'203'=>'Non-Authoritative Information',
'204'=>'No Content',
'205'=>'Reset Content',
'206'=>'Partial Content',
'300'=>'Multiple Choices',
'301'=>'Moved Permanently',
'302'=>'Found',
'303'=>'See Other',
'304'=>'Not Modified',
'305'=>'Use Proxy',
'307'=>'Temporary Redirect',
'400'=>'Bad Request',
'401'=>'Unauthorized',
'402'=>'Payment Required',
'403'=>'Forbidden',
'404'=>'Not Found',
'405'=>'Method Not Allowed',
'406'=>'Not Acceptable',
'407'=>'Proxy Authentication Required',
'408'=>'Request Timeout',
'409'=>'Conflict',
'410'=>'Gone',
'411'=>'Length Required',
'412'=>'Precondition Failed',
'413'=>'Request Entity Too Large',
'414'=>'Request-URI Too Long',
'415'=>'Unsupported Media Type',
'416'=>'Requested Range Not Satisfiable',
'417'=>'Expectation Failed',
'500'=>'Internal Server Error',
'501'=>'Not Implemented',
'502'=>'Bad Gateway',
'503'=>'Service Unavailable',
'504'=>'Gateway Timeout',
'505'=>'HTTP Version Not Supported'
);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment