03.07.09

Fix broken UTF8 encoded RSS feeds in php

- PHP, RSS -

Parsing feeds with the SimpleXML object is a walk in the park. But it can turn into a pain in the ass, when the provided XML feed isn’t correctly UTF8 encoded and I can tell you, there are some ugly ones out there in the wild.

Parsing RSS feeds with SimpleXML

Normally you can load and parse a feed as simple as:

<?
$feed = simplexml_load_file(rss.xml);

foreach ($feed->channel->item as $item){ ?>
  <h3><?= $item->title ?></h3>
  <p><?= $item->description ?></p>
<? } ?>

That’s a standard. Sometimes though the XML is not proper UTF8 encoded, and the following nasty error occurs loading in into the SimpleXML object.

Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 6: parser error : Input is not proper UTF-8, indicate encoding ! Bytes: 0xEB 0x6C 0x65 0x20 in index.php on line 33

How to fix this

First I tried to filter out the bad characters with str_replace, but that’s silly. I didn’t feel right. So I looked further and I stumbled on this quite unknow function iconv.

What’s the iconv module

This module contains an interface to iconv character set conversion facility. With this module, you can turn a string represented by a local character set into the one represented by another character set, which may be the Unicode character set. Supported character sets depend on the iconv implementation of your system.

The iconv function is available since PHP 4.0.5

Description

string iconv ( string $in_charset , string $out_charset , string $str )

Performs a character set conversion on the string str from in_charset to out_charset .

A better and safer way of parsing

To be on the safe side, never read in a feed directly into a SimpleXML object, but do it this way:

$feed = file_get_contents($feed_url);

$feed = iconv("UTF-8","UTF-8//IGNORE",$feed);

$feed = simplexml_load_string($feed); 

There is another reason to do this. Reading in a feed with file_get_contents is much faster then loading it straight into a SimpleXML object , at least in PHP < 5.2x. So following above instructions you will experience a performance gain and get rid of some nasty unexpected errors.

18.03.09

Dating takes time, women are difficult but PHP?

- Apache, Linux, PHP -

One of the big annoyances is having a blog or webpage in a certain language and that all the dates appear on your website in English. Of course you can choose just numbered formats, but sometimes the month or the day of the week in words adds that needed little extra polish.

Formatting a date in PHP

Normally a date is formatted like this with the date() function:

[sourcecode language=”php”]
< ?= date(‘d-m-Y’) ?>
[/sourcecode]

Formatting a localised date in PHP

But to format a date in your preferred language you need to use the strftime function:

Format the time and/or date according to locale settings. Month and weekday names and other language-dependent strings respect the current locale set with setlocale().

So we also need the setlocale() function. This function let you set the language to use for the formatting. Your system or server should have them installed, otherwise it’s not possible! And most of them are not installed by default, although easily available.
What does the setlocale() function do:

Returns the new current locale, or FALSE if the locale functionality is not implemented on your platform, the specified locale does not exist or the category name is invalid.
An invalid category name also causes a warning message. Category/locale names can be found in RFC 1766 and ISO 639. Different systems have different naming schemes for locales.

Tough pages to consume to be honest, but let’s go on.

Locales settings on your server

You can find the installed localisations in this file on a Linux server:

[sourcecode language=”xml”]
vi var/lib/locales/supported.d/local
[/sourcecode]

My server only had one line by default!
Simply add the one’s you’re missing. You can find all possible localisations in this file:

[sourcecode language=”xml”]
/usr/share/i18n/SUPPORTED
[/sourcecode]

Don’t add them all: it will decrease performance.

So we’re nearly done:

[sourcecode language=”php”]
< ?
setlocale(LC_TIME,’nl-NL.UTF-8′);
echo strftime(‘%s-$b-%y’,time())
?>
[/sourcecode]

You need to enter the correct encoding aswell, otherwise it won’t work. This is dependent of the server settings, so it will influence the portability of your code a bit.
Above code will give you the current date in Dutch, but we need another function to create localised dates from stored timestamps of the MySQL database.

Converting stored Timestamps to PHP understandable format

A MySQL timestamp value won’t work as a direct input to the strftime function. We need to convert it with another function: strtotime.

So we end up with something like this:

[sourcecode language=”php”]
< ?
setlocale(LC_TIME,’nl-NL.UTF-8′);
echo strftime(‘%A %d $b %y’,strtotime($row->timestamp))
?>
[/sourcecode]

Quite a long road to take for a boring date!

27.02.09

PHP 5.2.9 released

- PHP -

The PHP development team would like to announce the immediate availability of PHP 5.2.9.

This release focuses on improving the stability of the PHP 5.2.x branch with over 50 bug fixes, several of which are security related. All users of PHP are encouraged to upgrade to this release.

Security Enhancements and Fixes in PHP 5.2.9:

  • Fixed security issue in imagerotate(), background colour isn’t validated correctly with a non truecolour image. Reported by Hamid Ebadi, APA Laboratory (Fixes CVE-2008-5498). (Scott)
  • Fixed a crash on extract in zip when files or directories entry names contain a relative path. (Pierre)
  • Fixed explode() behavior with empty string to respect negative limit. (Shire)
  • Fixed a segfault when malformed string is passed to json_decode(). (Scott)

Further details about the PHP 5.2.9 can be found in the release announcement for 5.2.9, the full list of changes is available in the ChangeLog for PHP 5.
Read more here

27.01.09

Netbeans: Code Completion for the Kohana Framework

- PHP -

In some earlier posts I’ve described ways to get Netbeans code completion working for CodeIgniter. In this post I will show you how to do the same for Kohana.

Kohana is originally a PHP-5 only fork of CodeIgniter, but it has developed into much more. Now all code has been rewritten, so only the framework in concept is based on CodeIgniter. It has nice ORM, AUTH and Cache modules.

It’s easy to convert and actually I prefer Kohana, it’s more intuitive and extensible. The only drawback: Kohana needs PHP 5.2+, where CI can be used with PHP 4.

Kohana uses the PHP class autoload feature, no need to include class files manually, PHP will lookup the file on the included path automatically at the moment the class is mentioned.
So when you write.
$this->db = New Database
The Database class declaration file will be loaded by PHP automatically. This way of lading classes is also understandable for the auto completion parser of Netbeans.

This is in contrast to CodeIgniter, because there classes are loaded like:
$this->load->library('validation')

To achieve code completion for CodeIgniter in Netbeans we have to manually insert comments to help the auto completion parser map properties to classes:
* @property CI_DB_active_record $db

That’s not the case in Kohana, but there is another problem. Kohana uses Class suffixes. The classes are declared like Database_Core, but called like Database (without the _Core).
To evade that we grap to the netbeans_ci_code_autocompletion helper file trick. But here we don’t write the property in comments, we simple do this:
Class Database extends Database_Core{}
etc

You can see the discussion on which this post is based in the Kohana forum. There is even a script that will generate the file automatically, so it will do extensions aswell. Works for Zend, Netbeans and Eclipse.

You can find the script here:
http://www.mapledesign.co.uk/code/kohana-zend-autocomplete/
There is a nasty flaw though on the website, they mixed up names. The script should be activated by:
http://yoursite/zend_ide
And it will create a zend_autocomplete.php file in the cache directory. You can move that to the nbproject folder if you want.

27.01.09

Netbeans revisited: Code Completion for Code-igniter II

- PHP -

In an earlier post I described a way to achieve PHP code completion in Netbeans for the CodeIgniter framework. That way consisted of inserting property comments – which functioned as a helper for Netbeans to interprete the use – in every user application controller. This post will describe an even easier way, but first some comments about the earlier post.

I showed some ways to automate the inserting of the comments, although writing phpDocumentor comments in Netbeans is supported natively. Just type /** and completion will be available like explained here.

It’s a good custom to write comments in phpDoc style, it will help users to understand your coding, and it will offer an extremely easy way to publish documentation about your programs. Read here more about phpDOC.

On more remark, I wrote to include the CodeIgniter path in the Netbeans Global Include Path, it’s definitely better to do it on a project basis, so include it in the PHP Include Path in the Project Properties. Otherwise you will have CodeIgniter completion on projects that don’t use CI. 🙂

Now were getting to the core of this post. Probably the easiest way to achieve code completion is to put a file in the source path of Netbeans, but out of the CodeIgniter application or systems paths, let’s say a file called `netbeans_ci_code_completion.php`, but you can give it any name you want.

Make a text file with this content:
< ? /** * @property CI_Loader $load * @property CI_Form_validation $form_validation * @property CI_Input $input * @property CI_Email $email * @property CI_DB_active_record $db * @property CI_DB_forge $dbforge * @property CI_Table $table * @property CI_Session $session * @property CI_FTP $ftp * .... */ Class Controller { } ?>

It doens’t matter how you call it, as long it has a php extension and is seen by Netbeans but not CodeIgniter. You can save it in a folder called temp, or even in the nbproject (netbeans project folder) folder. I worked for me, and I’ve haven’t noticed any drawbacks yet.

For most of my projects I have this project folder structure:

/application
/error
/images
/nbproject
/scripts
/styles
index.php
.htaccess

Putting it in the nbproject folder has the advantage that the file will not by copied to the server automatically, because that folder is already marked to be excluded in the synchronization settings.

Categories
Archives
Links