Skip to content

Instantly share code, notes, and snippets.

View CodeNegar's full-sized avatar

CodeNegar CodeNegar

View GitHub Profile
@CodeNegar
CodeNegar / gist:3749097
Created September 19, 2012 11:12
PHP: mysql date to unix timestamp
<?php
function mysql_to_unix($time = '')
{
// YYYY-MM-DD HH:MM:SS
$time = str_replace(array('-', ':', ' '), '', $time);
// YYYYMMDDHHMMSS
return mktime(
substr($time, 8, 2),
substr($time, 10, 2),
substr($time, 12, 2),
@CodeNegar
CodeNegar / gist:3749116
Created September 19, 2012 11:15
PHP: force download file
<?php
function force_download($filename = '', $data = '')
{
if ($filename == '' OR $data == '')
{
return FALSE;
}
// Try to determine if the filename includes a file extension.
// We need it in order to set the MIME type
@CodeNegar
CodeNegar / gist:3771059
Created September 23, 2012 13:43
php: wordpress generate dynamic style sheet file
<?php
require_once('../../../../wp-load.php');
header("Content-type: text/css");
$days_to_cache = 10;
header('Expires: '.gmdate('D, d M Y H:i:s',time() + (60 * 60 * 24 * $days_to_cache)).' GMT');
$plugin_path = WP_PLUGIN_URL.'/'.str_replace(basename( __FILE__),"",plugin_basename(__FILE__)); // plugin folder ex: http://localhost/wordpress/wp-content/plugins/dynamic-style/
$some_option = get_option('blogname');
$some_data = 450;
?>
.my_half_width_class { width: <?php echo $some_data; ?>px; }
@CodeNegar
CodeNegar / gist:3794225
Created September 27, 2012 14:11
php: fa to en and en to fa number
<?php
function fadigits($text)
{
$fa = array('۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹');
$en = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
return str_replace($en, $fa, $text);
}
function endigits($text)
{
$fa = array('۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹');
@CodeNegar
CodeNegar / gist:3819205
Created October 2, 2012 13:35
PH: Filters test
<h1>Filter Tests</h1>
<fieldset>
<legend><a href="<?php echo $_SERVER['PHP_SELF'] ?>">Demo Form</a></legend>
<form name="testForm" id="testForm" action="" method="get">
<input type="text" name="value" id="value" value="" />
<input type="submit" value="Submit" />
</form>
<div id="examples">
<strong>Try these examples:</strong> <a href="?value=nettuts">nettuts</a> | <a href="?value=123456">123456</a> | <a href="?value=1">1</a> | <a href="?value=0">0</a> | <a href="?value=123.456">123.456</a> | <a href="?value=-123.456">-123.456</a> | <a href="?value=123,456">123,456</a> | <a href="?value=true">true</a> | <a href="?value=false">false</a> | <a href="?value=http://net.tutsplus.com">http://net.tutsplus.com</a> | <a href="?value=http://net.tuts®plus.com">http://net.tuts®plus.com</a> | <a href="?value=192.168.0.1">192.168.0.1</a> | <a href="?value=1.2.3.4.5.6.7.8.9">1.2.3.4.5.6.7.8.9</a> | <a href="[email protected]">[email protected]</a> | <a href="?value=t(e)[email protected]">t(e)[email protected]</a> | <a href="
@CodeNegar
CodeNegar / gist:3819345
Created October 2, 2012 13:54
PHP: Enable GZip
<?php
ob_start("ob_gzhandler");
?>
<div><p>Lorem ipsum dolor sit amet tellus.</p></div>
<div><blockquote>Etiam quis nulla pretium et.</blockquote></div>
<div><img src="images/inset.png" alt="Inset Image" /></div>
<ul>
<li><p>Lorem ipsum dolor sit amet tellus.</p></li>
<li><blockquote>Etiam quis nulla pretium et.</blockquote></li>
<li><img src="images/inset.png" alt="Inset Image" /></li>
@CodeNegar
CodeNegar / gist:3832912
Created October 4, 2012 10:46
PHP: Wordpress this plugin url
WP_PLUGIN_URL . "/" . plugin_basename( dirname(__FILE__));
@CodeNegar
CodeNegar / gist:3888311
Created October 14, 2012 11:36
PHP: Wordpress change logout redirect url
<?php
add_filter( 'logout_url', 'custom_logout_url' );
function custom_logout_url( $default )
{
return add_query_arg('redirect_to', get_bloginfo('url'),$default);
}
?>
@CodeNegar
CodeNegar / gist:3939061
Created October 23, 2012 14:29
Java: multiple string replace
//array to hold replacements
String[][] replacements = {{"call me", "cm"},
{"as soon as possible", "asap"}};
//loop over the array and replace
String strOutput = inString;
for(String[] replacement: replacements) {
strOutput = strOutput.replace(replacement[0], replacement[1]);
}
@CodeNegar
CodeNegar / gist:3942253
Created October 23, 2012 22:44
PHP: Wordpress add custom meta box
<?php
// Hook into WordPress
add_action( 'admin_init', 'add_custom_metabox' );
add_action( 'save_post', 'save_custom_url' );
/**
* Add meta box
*/
function add_custom_metabox() {