Tech Thought

Tech tips, hints, and general musings. PHP, Perl, Mysql, Javascript, AJAX, JSON, Linux, Mac OSX

Entries Tagged ‘Php’

Countries of the world in PHP array

A handy snippet to share with anyone who needs access to the countries of the world (Australia/New Zealand at the top in my example) as a PHP list:

Leave a Comment

How To: PHP OAuth Twitter

So, you’re building a web based twitter client in PHP.  Easy, just use a simple Twitter API client and call the Twitter API directly.  But what if you don’t want to store the user’s Twitter authentication information anywhere?  It’s a security risk anyway and some people don’t want to give up their account details to a [...]

Comments (3)

How-To: Create a mysql friendly date with php

This is really simple – and quite useful if you are trying to insert a date into mysql and you can’t use the “now()” mysql command (due to say a timezone difference): $date = date(“Y-m-d G:i:s”);

Leave a Comment

How-To: Removing trailing whitespace from a string with php

Need to remove the trailing whitespace characters from a string in php? chomp() is how you do it in Perl, however in PHP it’s just as easy. Simply use rtrim(): // $string = “|my string with trailing whitespace |” $string = rtrim($string); // $string = “|my string with trailing whitespace|” It’s that easy! There is [...]

Leave a Comment

How-To: Read an RSS Feed with PHP

An easy way to read an RSS feed with PHP is to use the MagpieRSS library. This provides a simple API to read an RSS feed, and elegantly deal with the issues of timeouts and caching. It’s as simple as: require(‘rss_fetch.inc’); $rss = fetch_rss($url); echo “Site: “, $rss->channel['title'], “<br> “; foreach ($rss->items as $item ) [...]

Leave a Comment

How-To: Determine Real hostname in php

If you need to know the real host name of your server using php, use the following command: <? echo gethostbyaddr (gethostbyname ($_SERVER["SERVER_NAME"])); ?> This will return the server’s hostname, not the address that the script is being accessed by. Very useful if you have a script that needs to run on multiple servers.

Leave a Comment

How-To: Fix SimpleXML CDATA problem in php

If you’ve used the SimpleXML functions in PHP, you may have noticed some strange things happening with CDATA values in your XML file/string. All I needed to do was extract the value of my CDATA fields, however these were always coming back blank in the structure that simplexml_load_file returns. Finally, after hours of trawling google, [...]

Comments (14)

How-To: Parse XML with PHP (SimpleXML)

I’ve been looking for a simple way to convert XML into an array in PHP, like the Perl library XML::Simple does.  The best bet with PHP is SimpleXML, which provides a pretty simple interface for parsing XML in PHP. Take a simple example XML file: <list type=’randomlist’> <item> <name>Item Number 1</name> <code>ITEM1</code> </item> </list> To [...]

Leave a Comment

PHP Date in Human Readable Form (Facebook Style)

I like to display dates relative to now, aka Facebook.  So instead of displaying a date in the form 3-2-2008 (which can get confusing if you are in the US/Europe etc with dates being switched) I like to show it as 2 weeks ago as this is far easier to read. So I came accross [...]

Comments (4)

Upgrading PHP on Fedora Core 5 (or other distro)

Download the RPMs you require for your new PHP installation from the following URLs. If you have RPMs that aren’t listed, use the search function: php-5.2.5 pcre-6.6 php-cli-5.2.5 php-common-5.2.5 php-pear-1.7.1 php-gd-5.2.5 php-pd-5.2.5 php-mysql-5.2.5 sqllite Or if you are upgrading another distrobution, search for the equivalents here. Check which PHP RPMs you currently have installed: rpm [...]

Comments (4)

Installing Crypt::Blowfish on Windows from PPD

I have had a requirement to encrypt in Perl and decrypt in Php. As a result, I’ve been looking at ways to get this to work. There are a number of people who have managed to get this working using the Crypt::Blowfish module in perl and mcrypt_cbc function in Php. That’s great unless you are [...]

Comments (1)

php $_SESSION variable doesn’t stay persistent

Recently I was creating a new website from scratch, which required authentication and the use of PHP’s sessions functionality. I used the session to store information which I needed to be persistent between pages: Page 1 Page 2 However page 2 would never display the user ID value. I couldn’t work out what was causing [...]

Leave a Comment

“Foreach” style loop syntax for Perl, Php and Javascript

I am always forgetting the syntax for foreach() loops in javascript. They actually don’t exist (not in their Perl or Php form anyway) and they differ in that they iterate over all the properties of an object, not an array. You can get some unexpected results if you aren’t careful. In perl you do the [...]

Comments (2)

Problem with PHP json_decode() and prototype.js .toJSON() function

I have been tearing my hair out trying to work out why PHP’s json_decode() function wouldn’t convert a JSON string sent via AJAX using the prototype.js .toJSON() function. I was sending the string: {\”Assembly_ID\”: 1} However json_decode() in php would return NULL. After some fiddling around, I determined that json_decode() doesn’t like the “\” characters. [...]

Comments (3)

Data::Dumper for PHP

Along the same lines of my previous post, I was looking for a PHP equivalent to Data::Dumper for Perl. Php has a builtin function called var_dump() which will give you some fairly ugly output about your array/hash/object, however I was looking for something a little more elegant.Thankfully the folks over at Pear have come up [...]

Comments (1)