Flattening an array in PHP

php

You might have a multidimensional array in PHP that you might need to flatten to make it easier to work with. Let’s take the example “myArray”.

As you see from the code block below, you can create a temporary object and then from there you use array_walk_recursive to flatten the array into an easy to use format.


$objTmp = (object) array('aFlat' => array());
array_walk_recursive($myArray, create_function('&$v, $k, &$t', '$t->aFlat[] = $v;'), $objTmp);

Setup LAMP and phpMyAdmin from scratch on Ubuntu 10+

apache, lamp, linux, mysql, php, phpmyadmin

It’s really easy to get a LAMP webserver up and running, so I thought I’d post some nice easy instructions for installing on a Ubuntu box.

1) sudo apt-get install mysql-server mysql-client
-> you will need to choose a MySQL Root user password

2) sudo apt-get install apache2
-> confirm in your browser that you are able to view a page for your IP address. This could be localhost, or your local IP address.

3) sudo apt-get install php5 libapache2-mod-php5
-> you’re going to need to install PHP next as above.

4) /etc/init.d/apache2 restart
-> …and of course an Apache restart so that PHP is activated

5) sudo apt-get install phpmyadmin
-> you’re going to be asked which webserver to configure. go for apache2. it’ll then probably request your password you selected for MySQL.

6) vim /etc/apache2/apache2.conf
-> use your favorite editor to open up the apache config file

7) Include /etc/phpmyadmin/apache.conf
-> add the following somewhere in the file.

8) /etc/init.d/apache2 restart

9) http://yourhomepage.com/phpmyadmin
-> test 🙂

In PHP, converting Shift-JIS into UTF-8

php

Still some websites use Shift-JIS character sets instead of UTF-8. To convert Shift-JIS Japanese text into a more friendly UTF-8 format, I needed to include the following in my PHP code:

$converted_string = iconv(‘shift-jis‘,’utf-8‘.’//TRANSLIT’,$string_to_convert);

Of course it’s also possible to store the data in MySQL using a BLOB data type, but I wanted to use standard text/varchar, and make it searchable. This worked well.

The same can also be applied for converting the other way, when needing to convert UTF-8 to Shift_JIS.

$converted_string = iconv(‘utf8‘,’shift-jis‘.’//TRANSLIT’,$string_to_convert);

Discovering Blog Ping Services

curl, php, SEO

When driving traffic to a website, the continuous battle is the constant chopping and changing of your page and content in order to create the perfect SEO formula. I have always been interested how WordPress and Blogspot pages were able to receive traffic so quickly, and so much of it, without any kind of special optimizing of the page. Sure enough, Google has built a smart algorithm that will index and extract content from these blogs, but I couldn’t work out how search engines “knew” new content has been published on a Blog.

I have recently found out about “blog ping services” today, and spent some time reading up on these. Basically, a blog ping service will notify search engines and aggregators when there has been new content published on your page. Very smart! In addition to that, content is indexed very quickly and in many cases will filter through into blog aggregators such as Google blogs and Yahoo! blogs.

So, in PHP, how did I achieve this task? I started out with pingomatic. If you visit pingomatic’s homepage, you will see the different search engines that this blog ping delivers notifications to.

The next step was to integrate this into my code. Firstly, build your pingomatic URL, and keep it safe for later. Generally, the Pingomatic page will tell you to bookmark the page so that whenever you update your blog, you can visit that URL and Pingomatic’s harvester will deliver a message to all of the search engines.

Of course the most important job of a web developer is to strive for optimization and automation. We’re letting Pingomatic do the optimization for us, so now let’s focus on the automation. In my example below, this is a simple way to use PHP:

$webservice = "http://pingomatic.com/ping/?title=yourblog&blogurl=http%3A%2F%2Fwww.yourblog.com&rssurl=http%3A%2F%2Fwww.yourblog.com%2Ffootball_news.xml&chk_weblogscom=on&chk_blogs=on&chk_feedburner=on&chk_newsgator=on&chk_myyahoo=on&chk_pubsubcom=on&chk_blogdigger=on&chk_weblogalot=on&chk_newsisfree=on&chk_topicexchange=on&chk_google=on&chk_tailrank=on&chk_postrank=on&chk_skygrid=on&chk_collecta=on&chk_superfeedr=on&chk_audioweblogs=on&chk_rubhub=on&chk_a2b=on&chk_blogshares=on";
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_URL, $webservice);
 curl_setopt($ch, CURLOPT_HTTPHEADER,
 array('Content-type: text/xml'
 , 'Content-length: '.strlen($request)
 ,'User-Agent: Mozilla/5.0 (Windows; U;
 Windows NT 5.1; en-US; rv:1.9.1) Gecko/20090624 Firefox/3.5 (.NET CLR 3.5.30729' ));
 curl_setopt($ch, CURLOPT_POST, true);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
 $result = curl_exec($ch);

Quite simply, if you embed this code into your PHP code, a curl request will be sent to Pingomatic notifying their harvester that there is new content on your page. Done – enjoy the new traffic!