Tag Archive for to

Object to array

function object_to_array($object){
	$new = NULL;
	if (is_object($object)) {
		$object = (array) $object;
	}
	if (is_array($object)) {
		$new = array();
		foreach ($object as $key => $val) {
			$key = preg_replace("/^(.*)/", "", $key);
			$new[$key] = $this->object_to_array($val);
		}
	} else {
		$new = $object;
	}
	return $new;
}

source

Function to convert dates to and from Mysql

<?php

/**
* function dateconvert
*
* dateconvert is a handy function to take the aches and pains out of mysqls stupidity
* by converting data from a variable (posted from a form or just stored)
* into a format mysql will be able to store and converting the
* database date back into the british standard of date month year.
* The Script accepts day.month.year or day/month/year or day-month-year.
* example:
*
* <code>
* <?php // using type 1
* $date = "19.12.2005";
* $date = dateconvert($date, 1);
* echo $date; // Would echo 2005-12-19 which is the format stored by mysql
* ?>
* </code>
* <code>
* <?php // using type 2
* $date = $row['date']; //your mysql date
* $realdate = dateconvert($date,2);
* echo $realdate; // would display 19/12/2005
* ?>
* </code>
*
* @author Chris McKee <<a href="mailto:pcdevils@gmail.com">pcdevils@gmail.com</a>>
*
* @param string $date - Date to be converted
* @param string $func - which function is to be used (1 for input to mysql, 2 for output from mysql)
*/
function dateconvert($date,$func) {
if ($func == 1){ //insert conversion
list($day, $month, $year) = split('[/.-]', $date);
$date = "$year-$month-$day";
return $date;
}
if ($func == 2){ //output conversion
list($year, $month, $day) = split('[-.]', $date);
$date = "$day/$month/$year";
return $date;
}

}

?>

source

Hex to RGB

function hex2rgb($hex){
$rgb = array();
$rgb['r'] = hexdec(substr($hex, 0, 2));
$rgb['g'] = hexdec(substr($hex, 2, 2));
$rgb['b'] = hexdec(substr($hex, 4, 2));
return $rgb;
}

Usage:
print_r(hex2rgb('#009999'));
Returns:
Array
(
[r] => 0
[g] => 9
[b] => 153
)

source

Text String Clipper class

/**
Text String Clipper class
author: Blaine Joubert
version: 0.1
modified: 04/08/2009


Adds a ... to the end of a text string if its too long

To use: Import Class then use
var textClip:TextClipper = new TextClipper (cutOffCharLength, textFieldName, textFieldCharLength, textContent)

*/

package {
import flash.display.*;
import flash.text.*;


public class TextClipper extends MovieClip {

//--------------------------------------
// PUBLIC VARIABLES
//--------------------------------------

public var _cutOffCharLength:int
public var _textFieldName:TextField
public var _textFieldCharLength :int
public var _textContent:String

//--------------------------------------
// CONSTRUCTOR
//--------------------------------------

public function TextClipper(cutOffCharLength:int, textFieldName:TextField, textFieldCharLength:int, textContent:String ):void {

_cutOffCharLength       =  cutOffCharLength;
_textFieldName   		=  textFieldName;
_textFieldCharLength 	=  textFieldCharLength;
_textContent     		=  textContent;

Init();

}

public function Init():void {



if (_textFieldCharLength >= _cutOffCharLength) {

_textFieldName.replaceText(_cutOffCharLength, _textFieldCharLength, "...");

}else {

_textFieldName.text =  _textContent
}

}
}

}

source

AS3 – Check to see if a string is true /false

//Recieves a string, checks to see if it is true/false, passes boolean back
function checkBoolean(val:String):Boolean {
if (val.toLowerCase() == "true") {
return true;
} else {
return false;
}
}

source

add spans to links drupal

function theme_links($links, $attributes = array('class' => 'links')) {
$output = '';

if (count($links) > 0) {
$output = '<ul'. drupal_attributes($attributes) .'>';

$num_links = count($links);
$i = 1;

foreach ($links as $key => $link) {
$class = $key;

// Add first, last and active classes to the list of links to help out themers.
if ($i == 1) {
$class .= ' first';
}
if ($i == $num_links) {
$class .= ' last';
}
if (isset($link['href']) && $link['href'] == $_GET['q']) {
$class .= ' active';
}
$output .= '<li class="'. $class .'">';

if (isset($link['href'])) {
// Pass in $link as $options, they share the same keys.
$link['html'] = TRUE;
$output .= l('<span>'. $link['title'] .'</span>', $link['href'], $link);
}
else if (!empty($link['title'])) {
// Some links are actually not links, but we wrap these in <span> for adding title and class attributes
if (empty($link['html'])) {
$link['title'] = check_plain($link['title']);
}
$span_attributes = '';
if (isset($link['attributes'])) {
$span_attributes = drupal_attributes($link['attributes']);
}
$output .= '<span'. $span_attributes .'>'. $link['title'] .'</span>';
}

$i++;
$output .= "</li>n";
}

$output .= '</ul>';
}

return $output;
}

source

Convert String to Proper Case

function strProper($str) {
$noUp = array('a','an','of','the','are','at','in');
$str = trim($str);
$str = strtoupper($str[0]) . strtolower(substr($str, 1));
for($i=1; $i<strlen($str)-1; ++$i) {
if($str[$i]==' ') {
for($j=$i+1; $j<strlen($str) && $str[$j]!=' '; ++$j); //find next space
$size = $j-$i-1;
$shortWord = false;
if($size<=3) {
$theWord = substr($str,$i+1,$size);
for($j=0; $j<count($noUp) && !$shortWord; ++$j)
if($theWord==$noUp[$j])
$shortWord = true;
}
if( !$shortWord )
$str = substr($str, 0, $i+1) . strtoupper($str[$i+1]) . substr($str, $i+2);
}
$i+=$size;
}
return $str;
}
$str = "blah blah";
echo strProper($str);

source

Away3D Local 3d to Global 2d point

var sv:ScreenVertex = camera.screen(object3d);
var globalX:Number = sc.x + view.x;
var globalY:Number = sc.y + view.y;

source

PHP Create Google Map Directions Link

function googleMapLink($to, $from) {
$destinationAddy = '&daddr='.urlencode($to->getAddress().' '.$to->getCity().' '.$to->getState());

if($from) {
$startAddy = '&saddr='.urlencode($from->getAddress().' '.$from->getCity().' '.$from->getState());
} else {
$startAddy = '&saddr=';
}

return htmlentities("http://maps.google.com/maps?f=q&amp;hl=en&amp;{$startAddy}{$destinationAddy}");
}

source

Array to String

//coded by cobra90nj

var str = "";

var arrname = new Array(5);
arrname[0] = "Hi, ";
arrname[1] = "this ";
arrname[2] = "is ";
arrname[3] = "an ";
arrname[4] = "example.";

function arrtostr(arry)
{
for (var n in arry) {
str += arry[n];
}

return str;
}

alert(this.arrtostr(arrname));

source