Tag Archive for php

Assign POST/GET to internal array

<?php
// Loop through the fields in the POST and assign them to our internal variable.
// There are several special cases we need to handle differently, so we'll skip
// them for now.
foreach($_POST as $key=>$value) {
switch($key) {
case 'submit'; // Completely skip over the submit button
case "field_1";
case "/^a regex$/";
case "field_20";
default:
$myArray[$key] = $value;
}
}
?>

source

HTML IEで表示させないコメントアウト

<!--[if !IE]> コメント <![endif]-->

source

Get php.ini location

php -i | grep php.ini

source

Reg Expresion

<?php
$string = "first.last@domain.co.uk";
if (preg_match(‘/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2}$/’,
$string))
{
echo "example successful.";
}
?>

source

PHP Plain date format

/**
* Formatta una data dal formato yyyy-mm-dd hh:mm:ss in yyyymmddhhmmss
*/
function plainDate( $d ) {
$pd = explode(' ', $d);
$dd = explode('-', $pd[0]);
$hp = explode(':', $pd[1]);
return( $dd[0].$dd[1].$dd[2].$hp[0].$hp[1].$hp[2] );
}

source

PHP word cut

/**
* String word cut
*
* @private
*/
function _wpmutility_wordCut($content, $limit){
$content = explode(' ',$content);
for($i=0; $i<$limit; $i++) $summary[$i] = $content[$i];
$summary = implode(' ', $summary).'...';
return $summary;
}

source

md5 hash_hmac for php4

function hmac ($key, $data)
{
// RFC 2104 HMAC implementation for php.
// Creates an md5 HMAC.
// Eliminates the need to install mhash to compute a HMAC
// Hacked by Lance Rushing

$b = 64; // byte length for md5
if (strlen($key) > $b) {
$key = pack("H*",md5($key));
}
$key  = str_pad($key, $b, chr(0x00));
$ipad = str_pad('', $b, chr(0x36));
$opad = str_pad('', $b, chr(0x5c));
$k_ipad = $key ^ $ipad ;
$k_opad = $key ^ $opad;

return md5($k_opad  . pack("H*",md5($k_ipad . $data)));
}

source

PHP Zip file creation, OOP

<?php
/*
By: 		Matt Ford

Purpose:	Basic class to create zipfiles
*/

class zipFile {
public $files = array();
public $settings = NULL;
public $fileInfo = array (
"name" => "",
"numFiles" => 0,
"fullFilePath" => ""
);
private $fileHash = "";
private $zip = "";

public function __construct($settings) {
$this->zipFile($settings);
}

public function zipFile($settings) {
$this->zip = new ZipArchive();
$this->settings = new stdClass();

foreach ($settings as $k => $v) {
$this->settings->$k = $v;
}
}

public function create() {
$this->fileHash = md5(implode(",", $this->files));
$this->fileInfo["name"] = $this->fileHash . ".zip";
$this->fileInfo["numFiles"] = count($this->files);
$this->fileInfo["fullFilePath"] = $this->settings->path . "/" . $this->fileInfo["name"];
if (file_exists($this->fileInfo["fullFilePath"])) {
return array (
false,
"already created: " . $this->fileInfo["fullFilePath"]
);
}
else {
$this->zip->open($this->fileInfo["fullFilePath"], ZIPARCHIVE::CREATE);
$this->addFiles();
$this->zip->close();
return array (
true,
"new file created: " . $this->fileInfo["fullFilePath"]
);
}
}

private function addFiles() {
foreach ($this->files as $k) {
$this->zip->addFile($k, basename($k));
}
}
}

$settings = array (
"path" => dirname(__FILE__)
);

$zipFile = new zipFile($settings);

$zipFile->files = array (
"./images/navoff.jpg",
"./images/navon.jpg"
);

list($success, $error) = $zipFile->create();

if ($success === true) {
//success
}
else {
//error because: $error
}
?>

source

A PHP function to return the first N words from a string

function shorten_string($string, $wordsreturned)
/*  Returns the first $wordsreturned out of $string.  If string
contains more words than $wordsreturned, the entire string
is returned.*/
{
$retval = $string;	//	Just in case of a problem
$array = explode(" ", $string);
/*  Already short enough, return the whole thing*/
if (count($array)<=$wordsreturned)
{
$retval = $string;
}
/*  Need to chop of some words*/
else
{
array_splice($array, $wordsreturned);
$retval = implode(" ", $array)." ...";
}
return $retval;
}

source

Dump Array

{$foo|@print_r}

source