Archive for the ‘PHP’ Category

PHP – array_map + create_function

Sunday, September 20th, 2009

For use with php < 5.3. If you're using 5.3 just use a proper anonymous function.

array_map(create_function(‘$v’, ‘return strtolower($v).”s”;’), $a);

Netbeans + CakePHP – syntax highlighting for .ctp files

Thursday, August 6th, 2009

Tools -> Options -> Miscellaneous -> Files

New File Extension (ctp)

Associate with PHP

Viola! CTP files are now treated as PHP files!

PHP – Split with delimiter caputre

Thursday, July 23rd, 2009

Split a string on a regular expression and also capture the delimiter.  This can be useful if you have regular expressions like /(cat|dog|fish)/.

$arr = preg_split($rx, $content, -1, PREG_SPLIT_DELIM_CAPTURE);
for ($i = 0; $i < count($arr); $i+=2) {
  $chunk = $arr[0];
  $delim = $arr[1];
}

Dreamweaver mx2004 code color CakePHP template files – .thtml .ctp

Sunday, June 29th, 2008

You want to get dreamweaver to treat .thtml and .ctp files like php files. There are 2 config files that you’ll need to edit

View hidden file types

C:\Documents and Settings\[user]\Application Data\Macromedia\Dreamweaver MX 2004\Configuration\Extensions.txt

PHP,PHP3,PHP4,TPL,THTML,CTP:PHP Files

C:\Program Files\Macromedia\Dreamweaver MX 2004\Configuration\DocumentTypes\MMDocumentTypes.xml

<documenttype id=”PHP_MySQL” servermodel=”PHP MySQL” internaltype=”Dynamic” winfileextension=”php,php3,php4,thtml,ctp” macfileextension=”php,php3,php4,thtml,ctp” file=”Default.php” writebyteordermark=”false”>

Duplicating a database using PHP and mysqldump

Sunday, June 29th, 2008

Note i’m using VPS so there are no restrictions – you will only be able to do this on some shared hosts.

<?php

error_reporting(E_ALL);
include ‘../config/database.php’;
include ‘../vendors/MySQLConnection.php’; // personal wrapper, should be obvious what it’s doing
$config = new DATABASE_CONFIG();
$db_test = new MySQLConnection(
$config->test['host'],
$config->test['login'],
$config->test['password'],
$config->test['database']
);

ob_start();
system(‘mysqldump -h ‘.$config->default['host'].’ -u ‘.$config->default['login'].’ -p’.$config->default['password'].’ --compact --add-drop-table ‘.$config->default['database']);
$s = ob_get_clean();
$a = split(‘;’,$s);
function t(&$s){$s=trim($s);}
array_walk($a,’t');
foreach ($a as $sql) {
$db_test->query($sql);
}

?>