Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

Linux Ask!

Mar 222011
 

Sending a UTF8 encoded CSV from PHP

Answer:

It is easy to generate a CSV file using the fputcsv function. One of a very common problems is even the file is UTF-8 encoded, Microsoft Excel is still unable to identiy the file as UTF-8.

To solve this, you need to add the UTF-8 BOM to the beginning of the file.

E.g.

<?php 

echo "\xEF\xBB\xBF" . $csvdata; // Assume $csvdata contains the CSV string you want to output
Mar 182011
 

Error control operator in PHP

Answer:

The error control operator (the @) is useful to ignore any error messages that might be generated by PHP.

E.g.

<?php

$my_value = @$my_array[$key];

The above statement will not generate any warning even if the index $key doesn't exist.

Mar 162011
 

Render web page using PyQt

Answer:

Qt bindings to WebKit have been added since 4.4, so you can use PyQt to script the browser (Webkit based).

Firstly, make sure you have installed the need packages:

# sudo apt-get install libqt4-core libqt4-webkit python-qt4

Then you can write a simple script to test, e.g.

#!/usr/bin/env python

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *

app = QApplication(sys.argv)

web = QWebView()
web.load(QUrl("http://www.google.com"))
web.show()

sys.exit(app.exec_())