A simple and easy to use PHP XML parser
The PHP XML parser:
1 class XML 2 { 3 static function parse($data, $handler, $encoding = "UTF-8") 4 { 5 $parser = xml_parser_create($encoding); 6 7 xml_set_object($parser, $handler); 8 9 xml_set_element_handler($parser, 10 array(&$handler, 'start'), 11 array(&$handler, 'end') 12 ); 13 14 xml_set_character_data_handler( 15 $parser, 16 array($handler, 'content') 17 ); 18 19 $result = xml_parse($parser, $data); 20 21 if(!$result) 22 { 23 $error_string = xml_error_string(xml_get_error_code($parser)); 24 $error_line = xml_get_current_line_number($parser); 25 $error_column = xml_get_current_column_number($parser); 26 27 $message = sprintf("XML error '%s' at line %d column %d", $error_string, $error_line, $error_column); 28 29 throw new Exception($message); 30 } 31 32 xml_parser_free($parser); 33 } 34 }
A result handler:
1 class ResultHandler 2 { 3 var $tag; 4 5 function start ($parser, $tagName, $attributes = null) 6 { 7 echo "start"; 8 $this->tag .= $tagName; # Use .= to work around bug... 9 } 10 11 function end ($parser, $tagName) 12 { 13 echo "end"; 14 $this->tag = null; 15 16 } 17 18 function content ($parser, $content) 19 { 20 echo "$this->tag: $content" ; 21 } 22 }
Then in your code:
1 $xml = "<a>bah</a>"; 2 XML::parse($xml, new ResultHandler());
Note that HTML /XML entities are considered to be tags by PHP ’s XML parser, so your start tag handler will be called three times for this tag, once for “really”, once for “&” and once for ” bad parser”:
1 <data>really & bad parser</data>
I guess this is a bug… You can
PHP file upload gotchas
PHP file upload works in mysterious ways:
1 http://us3.php.net/manual/en/ini.core.php#ini.post-max-size 2 http://fi.php.net/manual/en/features.file-upload.php#73762 3 http://de3.php.net/manual/en/features.file-upload.errors.php
How to execute a CakePHP controller's action from a cron job
First copy webroot/index.php to webroot/cron_scheduler.php. Replace everything below require CORE _PATH . ‘cake’ . DS . ‘bootstrap.php’; with the following code:
1 # 2 # BEGIN 3 # 4 # This was added to webroot/cron_scheduler.php 5 # 6 7 # Check that URI was specified and that we're called from the command line (not the web) 8 if($argc == 2 && php_sapi_name() === "cli") 9 { 10 # Set request URI 11 $_SERVER['REQUEST_URI'] = $argv[1]; 12 # Set user-agent, so we can do custom processing 13 $_SERVER['HTTP_USER_AGENT'] = 'cron'; 14 15 $Dispatcher= new Dispatcher(); 16 $Dispatcher->dispatch($argv[1]); 17 } 18 19 # 20 # END 21 # 22 # 23 #
Now you can execute CakePHP from the command line with the following command:
1 $ php cron_scheduler.php /controller/action
If you get the following error, remove the lines containing line feeds in bootstrap.php:
1 Warning: Cannot modify header information - headers already sent by (output started at .../app/config/b 2 ootstrap.php:48) in .../app/app_controller.php on line 46
How to generate a pronouncable password with PHP
This is a slight modification of a script I found at PHPFAQ :
1 class Password 2 { 3 static function generate($length) 4 { 5 srand((double)microtime()*1000000); 6 7 $vowels = array("a", "e", "i", "o", "u"); 8 $cons = array("b", "c", "d", "g", "h", "j", "k", "l", "m", "n", "p", "r", "s", "t", "u", "v", "w", "tr", 9 "cr", "br", "fr", "th", "dr", "ch", "ph", "wr", "st", "sp", "sw", "pr", "sl", "cl"); 10 11 $num_vowels = count($vowels); 12 $num_cons = count($cons); 13 14 $password = ''; 15 16 for($i = 0; $i < $length; $i++) 17 { 18 $password .= $cons[rand(0, $num_cons - 1)] . $vowels[rand(0, $num_vowels - 1)]; 19 } 20 21 return substr($password, 0, $length); 22 } 23 }
To use it try this code:
1 require 'password.php' 2 3 echo Password.generate(10);
It will spit out passwords that are semi-pronouncable.
How to send both HTML and text emails with PHP and PHPMailer
This is an example on how to use PHPMailer to send an HTML email. It also shows how to include a text only version of the same email for clients, such as mutt, that don’t support HTML emails.
1 $mail = new PHPMailer(); 2 3 $mail->IsHTML(true); 4 $mail->CharSet = "text/html; charset=UTF-8;"; 5 $mail->IsSMTP(); 6 7 $mail->WordWrap = 80; 8 $mail->Host = "smtp.thehost.com"; 9 $mail->SMTPAuth = false; 10 11 $mail->From = $from; 12 $mail->FromName = $from; // First name, last name 13 $mail->AddAddress($to, "First name last name"); 14 #$mail->AddReplyTo("reply@thehost.com", "Reply to address"); 15 16 $mail->Subject = $subject; 17 $mail->Body = $htmlMessage; 18 $mail->AltBody = $textMessage; # This automatically sets the email to multipart/alternative. This body can be read by mail clients that do not have HTML email capability such as mutt. 19 20 if(!$mail->Send()) 21 { 22 throw new Exception("Mailer Error: " . $mail->ErrorInfo); 23 }