Tag Archive for php

objects into array

function objectsIntoArray($arrObjData, $arrSkipIndices = array())
{
    $arrData = array();
   
    // if input is object, convert into array
    if (is_object($arrObjData)) {
        $arrObjData = get_object_vars($arrObjData);
    }
   
    if (is_array($arrObjData)) {
        foreach ($arrObjData as $index => $value) {
            if (is_object($value) || is_array($value)) {
                $value = objectsIntoArray($value, $arrSkipIndices); // recursive call
            }
            if (in_array($index, $arrSkipIndices)) {
                continue;
            }
            $arrData[$index] = $value;
        }
    }
    return $arrData;
}

source

WordPress Archives Basic

<ul><?php wp_get_archives('type=monthly'); ?></ul>

source

ENDOFSTRING PHP Strings

$string = <<<ENDOFSTRING 
This is a string 

It can include both 'single' and "double" quotes 
without needing to escape them. However, $variables 
will still be interpolated as they are in double 
quoted strings. Complex variable expressions such as 
{$array['element']} or {$object->property} can also 
be included and will be evaluated if they are included 
in curly braces (they may work without curly braces 
but I tend to include them for added clarity). The 
string will terminate with whatever you specified 
at the start like this: 
ENDOFSTRING;

source

Get Weather – Step 4 – Enhancing encapsulation

class WeatherService{

	public function WeatherService(){
		//some initialization...
	}
	
	public function getWeatherInfo($city, $method=""){
		
		$res = shell_exec("./getWdr.sh {$city}");
		
		if ($methos=="CL")
			return convertFrToC($res);
			
		return $res;
	}
	
	public static function convertFrToC($val){
		return (($val+40)*5/9)-40;
	}
	

}


//now it can also be used independently of the rest of the WeatherService 
//functions and main purpose...

$c = WeatherService::convertFrToC(22);

source

Compress CSS with PHP

<?php
    ob_start ("ob_gzhandler");
    header("Content-type: text/css; charset: UTF-8");
    header("Cache-Control: must-revalidate");
    $offset = 60 * 60 ;
    $ExpStr = "Expires: " .
    gmdate("D, d M Y H:i:s",
    time() + $offset) . " GMT";
    header($ExpStr);
?>

body { color: red; }

source

PHP Country Code to HTML Currency Symbol List

function country_currency( $amount = 0 ) {
    $bc = ''; // Set this to your country
    $currency_before = '';
    $currency_after = '';
    
    if( $bc == 'GB' || $bc == 'IE' || $bc == 'CY' ) $currency_before = '&pound;';
    if( $bc == 'AT' || $bc == 'BE' || $bc == 'FI' || $bc == 'FR' || 
        $bc == 'DE' || $bc == 'GR' || $bc == 'GP' || $bc == 'IT' ||
        $bc == 'LU' || $bc == 'NL' || $bc == 'PT' || $bc == 'SI' ||
        $bc == 'ES') $currency_before = '&euro;';
    if( $bc == 'BR' ) $currency_before = 'R$';
    if( $bc == 'CN' || $bc == 'JP' ) $currency_before = '&yen;';
    if( $bc == 'CR' ) $currency_before = '&cent;';
    if( $bc == 'HR' ) $currency_after = ' kn';
    if( $bc == 'CZ' ) $currency_after = ' kc';
    if( $bc == 'DK' ) $currency_before = 'DKK ';
    if( $bc == 'EE' ) $currency_after = ' EEK';
    if( $bc == 'HK' ) $currency_before = 'HK$';
    if( $bc == 'HU' ) $currency_after = ' Ft';
    if( $bc == 'IS' || $bc == 'SE' ) $currency_after = ' kr';
    if( $bc == 'IN' ) $currency_before = 'Rs. ';
    if( $bc == 'ID' ) $currency_before = 'Rp. ';
    if( $bc == 'IL' ) $currency_after = ' NIS';
    if( $bc == 'LV' ) $currency_before = 'Ls ';
    if( $bc == 'LT' ) $currency_after = ' Lt';
    if( $bc == 'MY' ) $currency_before = 'RM';
    if( $bc == 'MT' ) $currency_before = 'Lm';
    if( $bc == 'NO' ) $currency_before = 'kr ';
    if( $bc == 'PH' ) $currency_before = 'PHP';
    if( $bc == 'PL' ) $currency_after = ' z';
    if( $bc == 'RO' ) $currency_after = ' lei';
    if( $bc == 'RU' ) $currency_before = 'RUB';
    if( $bc == 'SK' ) $currency_after = ' Sk';
    if( $bc == 'ZA' ) $currency_before = 'R ';
    if( $bc == 'KR' ) $currency_before = 'W';
    if( $bc == 'CH' ) $currency_before = 'SFr. ';
    if( $bc == 'SY' ) $currency_after = ' SYP';
    if( $bc == 'TH' ) $currency_after = ' Bt';
    if( $bc == 'TT' ) $currency_before = 'TT$';
    if( $bc == 'TR' ) $currency_after = ' TL';
    if( $bc == 'AE' ) $currency_before = 'Dhs. ';
    if( $bc == 'VE' ) $currency_before = 'Bs. ';
    
    if( $currency_before == '' && $currency_after == '' ) $currency_before = '$';
    
    return $currency_before . number_format( $amount, 2 ) . $currency_after;
}

source

Elite RssPortal

Key points:

It can fetch articles from RSS, and then thanslate or convert them into new articles. 
It can generate more than 1000 NEW articles every day automatically. What you shall do in only find the right RSS source, then let the Elite RssPortal[ do the left.
Features:

Support Microsoft/Google translation engine. 
Support all kinds of language, such as Russon, Japan, French, Chinese, etc. 
Support all kinds of charsets, such as ANSI, EUC_JP, UTF-8, Big5, etc. 
Support fetch articles automatically.
Support extract article from a complex html page.
Support customized templates.

Details:

Very small, less than 500 KB. save your space. 
Support customization, you can add ad to the software and earn lots of money. 
All articles are stored in database, thus it's very fast. 
Static URL, easy to be cached by search engine. 
Good admin tools, you can add RSS easily.
Download:

download the software | PAD file | Screen shot | Demo site 1 | Demo site 2

Requirements:

Need Mysql server 5.0 or above. Need PHP 4.2 or above.

Installation:

Unzipped it and upload to your web server. Then navigate to your web site and follow the web guidance.

source

Fetch a Gravatar Image

<?php
$gravatar_link = '<a href="http://www.gravatar.com/avatar/'" >http://www.gravatar.com/avatar/'</a> . md5($email) . '?s=32';
echo '<img src="' . $gravatar_link . '" alt="gravatar"/>';
?>

source

php text to link

function replace_plain_text_link($plain_text) {
$url_html = preg_replace(
'/(?<!S)((http(s?)<a href="://)|(www.))+([w.1-9&=#?-~%;/]+)/'," >://)|(www.))+([w.1-9&=#?-~%;/]+)/',</a>
'<a href="http$3<a href="://$4$5">http$3://$4$5</a>'," >://$4$5">http$3://$4$5</a>',</a> $plain_text);
return ($url_html);
}
echo replace_plain_text_link("hi this is dummy text before
<a href="http://www.smjdesign.com" >http://www.smjdesign.com</a> hi this is dummy text after")

source

Php Feedback Form w/ Session and Browser Info

<? include("browser_class.php"); ?> //include class from here <a href="http://snipplr.com/view/35627/php-browser-detection-class/" >http://snipplr.com/view/35627/php-browser-detection-class/</a>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<a href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">" >http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"></a>
<html xmlns="<a href="http://www.w3.org/1999/xhtml">" >http://www.w3.org/1999/xhtml"></a>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link rel="stylesheet" href="attSearch.css" type="text/css" />
<style>
body {width:400px;margin:0px auto;text-align:left!important}
h2 {color:green;}
</style>
</head>

<body style="background:none!important;">
<?php
// declare values
$loc = $_COOKIE['location'];
$contact_email = $_POST['EmailAddress'];
$contact_subject = $_POST['Subject'];
$contact_name = $_POST['FullName'];
$contact_message = $_POST['Message'];
$contact_custname = $_SESSION['businessName'];
$contact_accno = $_SESSION['accountNo'];
$mydate = date ( 'l, F d Y g:i A',time()+240 );
// where to send e-mail to
$to = '<a href="mailto:youremail@yourprovider.com">youremail@yourprovider.com</a>';

// e-mail subject
$subject = "Feedback from $contact_custname";

// e-mail message
$message = "You have received feedback:
"."----------------------------------------------
"
."Contact Name: $contact_name
"
."Business Name: $contact_accno, $contact_custname
"
."Subject: $contact_subject
"
."Submitted: $mydate
"
."From IP: {$_SERVER['REMOTE_ADDR']}
"
."URL: $loc
"
."Browser: $Browser->Name $Browser->Version
"
."Message: $contact_message";

$headers = "From: $contact_name <$contact_email>
"
."Reply-To: $contact_email
"
."X-Mailer: PHP/".phpversion();

// check for validation, then send the e-mail
if(empty($contact_name) || empty($contact_email) || empty($contact_subject) || empty($contact_message)) {
echo '<h2>Have feedback?</h2>
<form method="post" action="">
<table id="Form-Details">
<tbody>
<tr><td width="20%">Your Name:</td><td><input type="text" name="FullName" size="40" /></td></tr>
<tr><td width="20%">Subject:</td><td><select name="Subject">
		<option value="Feedback">Feedback</option>
		<option value="Suggestion">Suggestion</option>
		<option value="Bug Report">Bug Report</option>
		<option value="Question">Question</option>
		</select>
</td></tr>
<tr><td width="20%">Email:</td><td colspan="3"><input type="text" name="EmailAddress" size="40" /></td></tr>
<tr><td colspan="4">Message:</td></tr>
<tr><td colspan="4"><textarea rows="6" name="Message" cols="47" class="input"></textarea></td></tr>
<tr><td colspan="4" align="right"><input type="submit" value="Submit Feedback" /></td></tr>
</tbody>
</table>
</form>';
} elseif(!ereg("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$", $contact_email)) {
echo "<h2 style='font-weight:bold;color:red;'>ERROR: Please enter a valid e-mail address.</h2>";
} else {
mail( $to, $subject, $message, $headers );
echo "<h2>Message Sent!</h2><br /><p>$contact_name,<br /><br />Thank you for your feedback, we will get back to you as soon as possible using $contact_email.";
}
?>
</body>
</html>

source