PHP 从del.icio.us链接生成blogroll

<?php
$username   = 'your_username';
$password   = 'your_password';
$cache_file = '/tmp/blogroll.xml';
 
// check for updates to del.icio.us account
$update = simplexml_load_file("https://{$username}:{$password}@api.del.icio.us/v1/posts/update");
 
if (strtotime($update['time']) > filemtime($cache_file))
{
    // del.icio.us has been updated since last cache; recache
    $data = file_get_contents("https://{$username}:{$password}@api.del.icio.us/v1/posts/all?tag=blogroll");
    file_put_contents($cache_file, $data);
}
 
// read links from cached del.icio.us data
$blogroll = simplexml_load_file($cache_file);
 
foreach ($blogroll->post as $blog)
{
    echo '<a href="' . htmlentities($blog['href']) . '">';
    echo htmlentities($blog['description']);
    echo "</a><br />\n";
}
?>

PHP 使用自动加载与PEAR

function __autoload($classname)
{
    // put the path to your class files here
    $my_path = "/var/www/mydomain.com/lib";

    // tell PHP to scan the default include paht AND your include path
    set_include_path(get_include_path() . PATH_SEPARATOR . $my_path);

    // name your classes and filenames with underscores, i.e., Net_Whois stored in Net_Whois.php
    $classfile = str_replace("_", "/", $classname) . ".php";

    include_once($classfile);
}

/**
 * EXAMPLE:
 * create one of your objects, saved in /var/www/mydomain.com/lib/Project/Database.php
 */
$db = new Project_Database();


/**
 * EXAMPLE:
 * create a PEAR object, saved in /usr/local/lib/php/File.php
 */

$f = new File();

PHP PHP - 多格式日期西班牙语

#Example:
get_date_spanish(time(), true, 'month'); # return Enero
get_date_spanish(time(), true, 'month_mini'); # return ENE
get_date_spanish(time(), true, 'Y'); # return 2007
get_date_spanish(time());#return 06 de septiempre, 12:31 hs



#Power by nicolaspar 2007 - especific proyect
function get_date_spanish( $time, $part = false, $formatDate = '' ){
    #Declare n compatible arrays
    $month = array("","enero", "febrero", "marzo", "abril", "mayo", "junio", "julio", "agosto", "septiempre", "diciembre");#n
    $month_execute = "n"; #format for array month

    $month_mini = array("","ENE", "FEB", "MAR", "ABR", "MAY", "JUN", "JUL", "AGO", "SEP", "DIC");#n
    $month_mini_execute = "n"; #format for array month

    $day = array("domingo","lunes","martes","miércoles","jueves","viernes","sábado"); #w
    $day_execute = "w";
    
    $day_mini = array("DOM","LUN","MAR","MIE","JUE","VIE","SAB"); #w
    $day_mini_execute = "w";

/*
Other examples:
    Whether it's a leap year
    $leapyear = array("Este año febrero tendrá 28 días"."Si, estamos en un año bisiesto, un día más para trabajar!"); #l
     $leapyear_execute = "L";
*/

    #Content array exception print "HOY", position content the name array. Duplicate value and key for optimization in comparative
    $print_hoy = array("month"=>"month", "month_mini"=>"month_mini");

    if( $part === false ){
        return date("d", $time) . " de " . $month[date("n",$time)] . ", ". date("H:i",$time) ." hs";
    }elseif( $part === true ){
        if( ! empty( $print_hoy[$formatDate] ) && date("d-m-Y", $time ) == date("d-m-Y") ) return "HOY"; #Exception HOY
        if( ! empty( ${$formatDate} ) && !empty( ${$formatDate}[date(${$formatDate.'_execute'},$time)] ) ) return ${$formatDate}[date(${$formatDate.'_execute'},$time)];
        else return date($formatDate, $time);
    }else{
        return date("d-m-Y H:i", $time);
    }
}

PHP 表格主题功能

/**
 * Themeing function to render a form as a table.
 *
 * The $form array should contain the following:
 * - $form['heading'], a form item of the default (markup) type (thus with
 *   only #value set), containing an (optional) heading.
 * - $form['header'], an array of form items of the default (markup) type
 *   (thus with only #value set), containing the header fields
 * - $form['rows'], an array of form items containing the rows. Each row is
 *   again an array of form items.
 * - $form['empty'], if this is set, then no table will be rendered. You may
 *   want to put an "empty message" in here.
 *
 * When $form['empty'] is not set, a table will be rendered first. After that,
 * all other form items will get rendered.
 * If $form['empty'] is set, $form['header'] and $form['rows] will be deleted,
 * and then all other form items will get rendered.
 */
function theme_mymodule_table_form($form) {
  $output = '';
  $header = array();
  $rows = array();

  // We always want the heading to be rendered first.
  $output .= drupal_render($form['heading']);

  if (!$form['empty']) {
    // Render $form['header'] as the header of the table.
    foreach ($form['header'] as $field) {
      $header[]['data'] = $field['#value'];
    }
    unset($form['header']);

    // Render $form['rows'] as the rows of the table.
    foreach (element_children($form['rows']) as $i) {
      $row = array();
      foreach (element_children($form['rows'][$i]) as $key) {
        $row[]['data'] = drupal_render($form['rows'][$i][$key]);
      }
      $rows[] = $row;
    }

    // Render the table.
    $output .= theme('table', $header, $rows, array('width' => '100%'));
  }
  else {
    // Delete the header and rows, just to be sure.
    unset($form['header']);
    unset($form['rows']);
    
    // Render the empty message.
    $output .= drupal_render($form['empty']);
  }

  // Render the remaining form items.
  $output .= drupal_render($form);

  return $output;
}

PHP 在symfony中使用您自己的对象填充object_select_tag

echo object_select_tag($domain, ‘getObjectId’, array (
‘related_class’ => ‘Object’,
‘peer_method’ => ‘getSortedObject’,
‘control_name’ => ‘object_id’,
‘include_blank’ => true,
));

in your ObjectPeer.php:

static public function getSortedObject() {
$c = new Criteria();
$c->addAscendingOrderByColumn(TablePeer::NAME);
$rs = TablePeer::doSelect($c);
return $rs;
}

and in your yml generator:
fields:
departement_id: { params: text_method =getNomCode peer_method =doSelectOrderByCode }

PHP MySQL DB Connect

$host = localhost;
$usr = 'user';
$pwd = 'pass';
    
#connect to database
$cid = mysql_connect($host,$usr,$pwd);
if (!$cid) { echo("ERROR: " . mysql_error() . "\n");    }

PHP 下一个和上一个链接/限制结果

$sort_by = ($_GET["sort_by"]) ? $_GET["sort_by"] : "created";
$sort_order = ($_GET["sort_order"]) ? $_GET["sort_order"] : "DESC";
$opposite_sort_order = ($sort_order == "ASC") ? "DESC" : "ASC";
$start = ($_GET["start"]) ? $_GET["start"] : 0;
$results_limit = 10;

...

$total_results = mysql_num_rows($result);
$query .= "ORDER BY " . $sort_by . " " . $sort_order . " LIMIT " . $start . ", " . $results_limit . ";";

...

if ($start > 0) {
		?>
        <p style="float: left;"><a href="?sort_by=<?=$sort_by?>&sort_order=<?=$sort_order?>&start=<?=$start - $results_limit?>">< Previous</a></p>
        <?php
	}
	if (($start + $results_limit) < $total_results) {
		?>
        <p style="float: right;"><a href="?sort_by=<?=$sort_by?>&sort_order=<?=$sort_order?>&start=<?=$start + $results_limit?>">Next ></a></p>
        <?php
	}

PHP 编程说明

<?php

// TODO:		Enter some task that needs to be done.
// FIXME:		Enter some bug that needs to be fixed.
// FIXME(BugNumber):	Reference a bug number for FIXME.
// CHANGED:		Once finished with TODO, replacement with CHANGED is recommended.
// NOTE:		Enter a simple note.
// OPTIMIZE:		A TODO specifically for optimization notes.
// IMPROVE:		A TODO specifically for improvement notes.

// Examples:

// TODO: Add in a update script.
// FIXME: Bug in update script (cannot download new file).
// FIXME(81239): Bug in update script (cannot download new file).
// CHANGED: Added update script.
// NOTE: The update script is still a bit buggy.
// OPTIMIZE: Update download speed.
// IMPROVE: Make the UI a bit better for update script.

?>

PHP 动态指数

// home.php?act=index

<?php

if (isset($_GET['act'])) $PAGEACTION = $_GET['act'];

else $PAGEACTION = 'index';

switch ($PAGEACTION) {

// index page
case 'index':
include ('index.php');
break;

// an about page
case 'about':
include ('about.php');
break;

// $PAGEACTION doesn't exist
default:
// or just forward to home.php?act=index
echo "page does not exist";
break;
}

?>

PHP 带有数组的strstr()和stristr()

<?php

function strstr_array( $haystack, $needle ) {
	if ( !is_array( $haystack ) ) {
		return false;
	}
	foreach ( $haystack as $element ) {
		if ( strstr( $element, $needle ) ) {
			return $element;
		}
	}
}

function stristr_array( $haystack, $needle ) {
	if ( !is_array( $haystack ) ) {
		return false;
	}
	foreach ( $haystack as $element ) {
		if ( stristr( $element, $needle ) ) {
			return $element;
		}
	}
}

?>