Write PHP Code Online

If you’re looking for a way to check if your PHP code is correct or not without ever loading your script to the web server, then try writecodeonline.com. Just paste your PHP code on the online form and click the ‘Run Code’ button and see if your code validates. The online interpreter defaults to PHP 5, but you can choose PHP 4 if you prefer.

I’ve used it several times to check if my logic makes sense. It’s a great tool to have. It’s very handy tool to have if you want to do a quick check if your code works or not. It does throw out errors so you’ll be able to correct your script if it has a syntax error or if your logic is incorrect.

Beginning with PHP 5.4, PHP comes with a built-in server. It’s meant to aid application development or for testing in controlled environments. It’s not meant to be a production server or placed in a public network.

Icecast PHP Class

I modified an existing Icecast PHP class that I found online. I’ve added a couple of extra fields particularly the ‘status’ field where you’ll be able to determine if your Icecast server is streaming or not. I’ve initially set the status variable to ‘On Air’ and ‘Off Air.’ To use the class, take a look at the code below:

// include the class file
include( 'icecast.php' );
// instantiate class
$stream = new IceCast();
// set server and mount
$server = 'http://yourdomain.com:8001';
$file   = '/status.xsl?mount=/yourmount.ogg';
// set the url
// get status info
$radio = $stream->getStatus();
// assign array to variables
// echo the status
echo $status.'<br/>';
// display more stats if ON AIR
if ($status=='ON AIR') :
echo $listeners.' listeners<br/>';
echo $most_listeners.' max listeners<br/>';

Here’s the Icecast class.

class IceCast {
    var $server = "http://yourdomain.com:8001";
    var $stats_file = "/status.xsl?mount=/yourmount.ogg";
    var $radio_info=array();
    function __construct() {
        // build array to store our Icecast stats   
        $this->radio_info['server'] = $this->server;
        $this->radio_info['title'] = '';
        $this->radio_info['description'] = '';
        $this->radio_info['content_type'] = '';
        $this->radio_info['mount_start'] = '';
        $this->radio_info['bit_rate'] = '';
        $this->radio_info['listeners'] = '';
        $this->radio_info['most_listeners'] = '';
        $this->radio_info['genre'] = '';
        $this->radio_info['url'] = '';
        $this->radio_info['now_playing'] = array();
        $this->radio_info['now_playing']['artist'] = 'Unknown';
        $this->radio_info['now_playing']['track'] = 'Unknown';
        $this->radio_info['status'] = 'OFF AIR';
    function setUrl($url,$file) {
        $this->radio_info['server'] = $this->server;
    private function fetch() {
        // create a new curl resource
        $ch = curl_init();
        // set the url
        // return as a string
        // $output = the status.xsl file
        $output = curl_exec($ch);
        // close curl resource to free up system resources
        return $output;
    function getStatus() {
        // loop through $output and sort arrays
        $temp_array = array();
        $search_for = "<td\s[^>]*class=\"streamdata\">(.*)<\/td>";
        $search_td = array('<td class="streamdata">','</td>');
        if(preg_match_all("/$search_for/siU",$output,$matches)) {
           foreach($matches[0] as $match) {
              $to_push = str_replace($search_td,'',$match);
              $to_push = trim($to_push);
        if(count($temp_array)) {
            //sort our temp array into our ral array
            $this->radio_info['title'] = $temp_array[0];
            $this->radio_info['description'] = $temp_array[1];
            $this->radio_info['content_type'] = $temp_array[2];
            $this->radio_info['mount_start'] = $temp_array[3];
            $this->radio_info['bit_rate'] = $temp_array[4];
            $this->radio_info['listeners'] = $temp_array[5];
            $this->radio_info['most_listeners'] = $temp_array[6];
            $this->radio_info['genre'] = $temp_array[7];
            $this->radio_info['url'] = $temp_array[8];
            if(isset($temp_array[9])) {
                $x = explode(" - ",$temp_array[9]);
                $this->radio_info['now_playing']['artist'] = $x[0];
                $this->radio_info['now_playing']['track'] = $x[1];
            $this->radio_info['status'] = 'ON AIR';
        return $this->radio_info;

Modifying the content of your blog

I wrote a custom blog in CodeIgniter two years ago. I revisited the code to change the content format a bit. I’ve noticed that there were several pieces of code that were commented out. At the time, I was playing around with what I could potentially do with the content. I thought I would share it here, because it will show you how you could drastically change the content of a blog by manipulating the content.

Let’s say the content of your blog is assigned to a variable called $item->content. We can alter the content by passing PHP functions to it. Let’s say we want to limit the content to just the first 330 characters. We can use a PHP function called substr. Substr returns a portion of the string from a specified start and length.

The Excerpt

$item->content = substr($item->content,0,330);
echo $item->content;

In this example, we are limiting content to the first 330 characters.

Remove page breaks and new lines

$item->content = str_replace(array('\r', '\n'), '', $item->content);
echo $item->content;

In this example, str_replace replace page breaks and new lines with ” or nothing.

Remove two spaces

$item->content = str_replace('  ', '', $item->content);
echo $item->content;

Finally, adding Read More [...]

$item->content = $item->content . '<a href=#>Read [...]</a>';
echo $item->content;

I appended to content a link with the anchor of ‘Read [...].’

Detect Device, OS, Browser

If you like to deliver specific content to a certain device type, you can use PHP’s server environment settings to know what type of device, operating systems, and browser type is being used. The server environment setting we are interested in is ‘HTTP_USER_AGENT.’ This index contains a string that is given off by the user agent, the browser in this case. All devices pass down pieces of information identifying themselves, including the type of device, operating system, and browser that is being used.

If we were to look for an iPad for example, we need to perform the following commands:

$iPad   = stripos($_SERVER['HTTP_USER_AGENT'],"iPad");
if ($iPad) { 
  echo 'This is an iPad'; 
} else { 
  echo 'This is not'; 

Other Devices

$user_agent = $_SERVER['HTTP_USER_AGENT'];
$iPad   = stripos($user_agent,"iPad");
$iPod   = stripos($user_agent,"iPod");
$iPhone = stripos($user_agent,"iPhone");
$webOS  = stripos($user_agent,"webOS");
$BlackBerry = stripos($user_agent,"BlackBerry");
$RimTablet= stripos($user_agent,"RIM Tablet");
if (stripos($user_agent,"Android") && stripos($user_agent,"mobile")) { 
  $Android = true;
} elseif(stripos($user_agent,"Android")){
  $AndroidTablet = true;
if ($AndroidTablet) {
  echo 'This is an Android Tablet';
} else {
  echo 'This is not an Android Tablet';

Detect OS Type

$user_agent = $_SERVER['HTTP_USER_AGENT'];
if (preg_match('/linux/i', $user_agent)) { $platform = 'linux'; }
if (preg_match('/macintosh|mac os x/i', $user_agent)) { $platform = 'mac'; }
if (preg_match('/windows|win32/i', $user_agent)) { $platform = 'windows'; }
if ($platform == 'windows') {
  // do something

Detect Browser Type

$user_agent = $_SERVER['HTTP_USER_AGENT'];
if (preg_match('/MSIE/i',$user_agent)) { $browser = 'Internet Explorer'; }
if (preg_match('/Firefox/i',$user_agent)) { $browser = 'Mozilla Firefox'; }
if (preg_match('/Chrome/i',$user_agent)) { $browser = 'Google Chrome'; }
if (preg_match('/Safari/i',$user_agent)) { $browser = 'Apple Safari'; }
if (preg_match('/Opera/i',$user_agent)) { $browser = 'Opera'; }
if (preg_match('/Netscape/i',$user_agent)) { $browser = 'Netscape'; }
if ($browser == 'Google Chrome') {
  // do something

With the sample code above, you can pretty much single out any device and deliver specific content to it.

WordPress Bash Script

You’ve heard of the famous 5-minute WordPress installation. You might be able to shave off some considerable time if you try installing WordPress from the command line using a Bash script. So, here’s my stab at how to install WordPress from the Linux shell. To be able to do this, you need access to server’s command line or shell as it’s commonly known in the Linux world. Some hosting companies allows you to have access to shell, some don’t. So, your mileage may vary. In this exercise, you need access to it, if you want to perform these functions.

sudo wget http://wordpress.org/latest.tar.gz
sudo tar -xzvf latest.tar.gz
sudo mv wordpress/* ./
rm -rf wordpress
rm -f latest.tar.gz
echo "done!"

The sequence

The first line tells the system it’s a Bash script.
The second line downloads the latest WordPress.
The third line untags or unzips the compressed file.
The fourth line moves wordpress to the current directory.
The fifth line deletes the wordpress folder.
The sixth removes the compressed file.
The seventh, you echo “done” to the shell.

The fourth line needs a little bit of explaining. By default, when you uncompress the WordPress zipped file, it dumps all the files in the “wordpress” directory. It might be your intention to create a sub-folder called “wordpress”, but if that is not what you want to do, then you will need to move all the contents out of the ‘wordpress’ folder and place them in the current directory. That’s what I’m doing here. I’m just moving the WordPress files up one level. After the moving the files, I just delete the now empty ‘wordpress’ folder.

The echo ‘done’ to the screen is just to tell the user that the installation has completed. All you need to do from this point on is to go to your browser and complete the now famous 5-minute installation. Except that you’ll do it at least half the time. Maybe a quarter off the time.