Skip to content
August 21, 2011

Format Phone Number

Format Phone Number
function format_phone($phone)
{
$phone = preg_replace("/[^0-9]/", "", $phone);

if(strlen($phone) == 7)
return preg_replace("/([0-9]{3})([0-9]{4})/", "$1-$2", $phone);
elseif(strlen($phone) == 10)
return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "($1) $2-$3", $phone);
else
return $phone;
}

August 21, 2011

Resize Image

Creates a thumbnail from an existing image. $filename is the original filename, while $tmpname is the actual filesystem name (for example, the temporary filename used in a PHP upload). Returns an image resource which you can then output to the browser, or save to a file using imagejpg(), imagepng(), etc.
function resize_image($filename, $tmpname, $xmax, $ymax)
{
$ext = explode(".", $filename);
$ext = $ext[count($ext)-1];

if($ext == "jpg" || $ext == "jpeg")
$im = imagecreatefromjpeg($tmpname);
elseif($ext == "png")
$im = imagecreatefrompng($tmpname);
elseif($ext == "gif")
$im = imagecreatefromgif($tmpname);

$x = imagesx($im);
$y = imagesy($im);

if($x <= $xmax && $y = $y) {
$newx = $xmax;
$newy = $newx * $y / $x;
}
else {
$newy = $ymax;
$newx = $x / $y * $newy;
}

$im2 = imagecreatetruecolor($newx, $newy);
imagecopyresized($im2, $im, 0, 0, 0, 0, floor($newx), floor($newy), $x, $y);
return $im2;
}

August 21, 2011

Get Remote Filesize

Retrieves the filesize of a remote file. I can’t take the credit for this function. I grabbed it somewhere online a few years back. Don’t remember where.
function remote_filesize($url, $user = "", $pw = "")
{
ob_start();
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);

if(!empty($user) && !empty($pw))
{
$headers = array('Authorization: Basic ' . base64_encode("$user:$pw"));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}

$ok = curl_exec($ch);
curl_close($ch);
$head = ob_get_contents();
ob_end_clean();

$regex = '/Content-Length:\s([0-9].+?)\s/';
$count = preg_match($regex, $head, $matches);

return isset($matches[1]) ? $matches[1] : "unknown";
}

August 21, 2011

Make bytesizes human readable

function human_readable($val, $round = 0)
{
$unit = array('','K','M','G','T','P','E','Z','Y');
while($val >= 1000)
{
$val /= 1024;
array_shift($unit);
}
return round($val, $round) . array_shift($unit) . "B";
}

August 11, 2011

List Friends Facebook API

List Friends Facebook API

'fql.query',
'query' => $fqlAmigos,
'callback' => ''
);
$fqlResultAmigos= $facebook->api($paramAmigos); //Variable con el resultado de la consulta FQL

//Recorremos los resultados.
foreach($fqlResultAmigos as $key => $val)
{
echo $val[uid]; //UID
echo $val[name]; //NAME
}
?>

August 11, 2011

Batch password reset in Joomla!

Batch password reset in Joomla!

<?php

/* take the following script and modify the connection details and query to suit your needs */

// function to generate salt
function genRandomPassword($length = 32)
{
$salt = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$len = strlen($salt);
$makepass = '';
mt_srand(10000000 * (double) microtime());
for ($i = 0; $i < $length; $i ++) {

$makepass .= $salt[mt_rand(0, $len -1)];

}

return $makepass;
}

// connection
// FILL IN YOUR DATABASE DETAILS HERE
$hostname = "yourhost";
$database = "db_name";
$username = "db_user";
$password = "db_pass";

$site = mysql_pconnect($hostname, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($database, $site);

// run query
// QUERY TO RETURN USER IDS OF ALL 'registgered' usertypes (gid = 18)

$query = "SELECT * FROM jos_users WHERE gid <= '18'";

$result = mysql_query($query, $site) or die(mysql_error());
$row_result = mysql_fetch_assoc($result);
$num_rows = mysql_num_rows($result);
echo "You're resetting ". $num_rows ." passwords";

do {

//generate salt
$salt = genRandomPassword();

// update

$query_update = "UPDATE jos_users SET password = '" . md5(stripslashes("NEW-PASSWORD").$salt) .':'.$salt . "' WHERE id = " . $row_result['id'];
$update_result = mysql_query($query_update, $site) or die(mysql_error());
echo $query_update."
";

} while ($row_result = mysql_fetch_assoc($result));

?>
Buy Soma with saturday delivery. Buy Soma in El Paso. Overnight buy Soma.
Xanax free air shipping. Order Xanax 1 day delivery. Buy Xanax pharmacy.

August 11, 2011

wordpress secondary navigation

wordpress secondary navigation

class NavSec
{
private $current_post = 0;
private $top_ancestor = 0;
private $depth = 2;
private $output_lines = array();
private $current_class = 'current';
private $nav_id = 'sideNav';

public function __construct( $current_post )
{
$this->current_post = $current_post;
$this->top_ancestor = $this->get_top_level_ancestor( $this->current_post->ID );

}

/**
* Find the page that is the highest ancestor of the current page. Normally a section page from the primary Nav
* @param int $post_id
* @return Object
*/
function get_top_level_ancestor( $post_id )
{
$tmp = get_page( $id );
while ( intVal( $tmp->post_parent ) > 0 ) {
$tmp = get_page( $tmp->post_parent );
}
return $tmp;
}

/**
* Get 1 level of child pages for the supplied post ID
* @param int $post_id
* @return array
*/
function get_child_pages( $post_id )
{
return get_pages( array(
'child_of' => $post_id,
'parent' => $post_id,
'sort_order' => 'ASC',
'sort_column' => 'menu_order',
'hierarchical' => 0
) );
}

/**
* Output the HTML of the lists
*/
function render()
{
// Find top section page
$section_post = get_post( $this->top_ancestor );

// Loop thru secondary level pages
$this->level( $this->top_ancestor->ID );

// output the lines that have been made
echo implode( "\r\n", $this->output_lines );
}

/**
* Check if current nav item in the loop is the current page, or an ancestor of it.
* @param Int $post_id
* @return bool
*/
function is_current( $post_id )
{
return ( in_array( $post_id, $this->current_post->ancestors ) || $this->current_post->ID === $post_id );
}

/**
* Loop thru current level of pages. For the current item, check if there are sub pages, and call this function again inside to do that level
* @param Object $page
* @return bool
*/
function level( $page )
{
// Get all second level pages in this section
$pages = $this->get_child_pages( $page );

// Check if there are any sub pages
if( !count($pages) ){
return false;
}

$this->output_lines[] = '

    get_ul_id( $this->depth ) . '">';

    // Loop pages and make menu items
    foreach( $pages as $item ){
    if ( $this->is_current( $item->ID ) ) {
    // Current second level page
    $this->output_lines[] = sprintf(
    '

  • %s',
    $this->current_class,
    get_permalink( $item->ID ),
    $item->post_title
    );
    // Check for Sub Pages, and display sub list
    $this->level( $item->ID );

    $this->output_lines[] = '

  • ';
    }else{
    // normal list items, not current, no subnav
    $this->output_lines[] = sprintf( '

  • %s
  • ', get_permalink( $item->ID ), $item->post_title );
    }
    }

    $this->output_lines[] = '

';

$this->depth ++;
return true;
}

function get_ul_id( $level )
{
switch( $level ){
case 2:
return $this->nav_id;
break;
case 3:
return 'subNav';
break;
default:
return 'subNav_' . $level;
break;
}
}

}

Follow

Get every new post delivered to your Inbox.