Simple XML parsing with CLI PHP, character encoding problems

januari 7th, 2007 by ltz

When using PHP for creating html documents intended to be read by a web browser there’s the possibility to set character encoding within the html-heading, getting a extra way to handle the character presentation of the content. However when running PHP code through the CLI-PHP software for internal, server-side purposes handling data the character encoding might require a little bit more work. Reading XML-documents using the PHP ”xml_parser” and at the same time trusting the character encoding given in the XML-document header as shown below might not be enough in order to make sure you get the correct character encoding.

...xml version="1.0" encoding="iso-8859-1" ...

Be sure to set the encoding within the creation of the XML-parser.


$xml_parser = xml_parser_create();

xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
xml_parser_set_option($xml_parser, XML_OPTION_TARGET_ENCODING, "ISO-8859-1");

Posted in Programming | Kommentering avstängd

PHP IMAP mail fetching including attachments..

november 8th, 2006 by ltz

Took me a while to figure out how to search and fetch a single attachment from a POP mailbox, without ending up with 300+ lines of code.. Maybe this one could help someone else..

// Open pop account.
$mailbox = imap_open( $popaddr, $popuser, $poppasswd);

// Search for the message we are looking for..
$result = imap_search($mailbox, $searchsubject, SE_UID);

if ($result) {

storePDFAttachment(imap_fetchbody($mailbox,$result[0],2),$savefilename,$savefilepath);

} else {

exit(”message not found in mailbox yet..”);

}

// Close the connection.
imap_close($mailbox);

function storePDFAttachment( $content , $filename , $localfilepath ) {

$file = fopen($localfilepath.$filename, ‘w’);
fwrite($file, imap_base64($content));

fclose($file);
}


Posted in Programming | Kommentering avstängd

Implementing updating stock chart

oktober 28th, 2006 by ltz

Trying to update a html-page without having a) annoying refreshes of the entire page, b) a third party framework implementation (ie: Java/Flash/etc..). My experience is that a “third party” implementation more or less makes the best solution however there are alternatives. For instance a simple AJAX-based implementation of such a function would be possible. This also does have its drawbacks, but for now we could look at the possibilities.

Attached is one solution involving PHP and a standard OSI-certified AJAX-framework called Agent AJAX. Please note the drawbacks of this solution. If the client/server connection is poor, the transfers in AJAX might create problems for the Client. You can of course overcome this problem by simply thinking your solution through and for instance creating feedback to the user at any transaction that might fail.

Posted in Programming | Kommentering avstängd