Posts Tagged ‘php’

PHP Development with Linux

Tuesday 25th March, 2008 at 11:55 pm

Linux PenguinI switched over to Linux at work recently, mostly due to our servers running on Linux and the opportunity of directly interfacing with the servers made my job a hell of a lot easier. No longer being able to use my trusty Macromedia Dreamweaver to do my work, I had to try out a couple of different PHP development solutions until I found the one that was right for me and could keep up with me.

And the winner is… VIM.

After playing around with software which promised the world, some which didn’t deliver, I grew to like the hidden simplicity of VIM. The development team leader at my work urged me to work with VIM, as it was the most simple and yet most powerful system to develop PHP with, and after so many functional but flawed pieces of PHP software, I’m inclined to agree with him. Here’s a couple of different PHP development solutions I tested in my search for a PHP editor and the pros and cons of each that I found. It should be noted that in each time, I fell back to VIM to complete my work when the PHP software was fighting with me to keep me from doing my work. Read more »

PHP: Pretty Highly Pedantic

Monday 3rd December, 2007 at 1:35 am

PHP Pretty Highly Pendantic header

I had to come up with some bad meaning for the abbreviation PHP, which had pedantic in it, ‘cos that’s what is has been for me the past few days (yes, this is coming from a person who is a PHP web developer by trade). I’m still working on the new version of the Gamestah website, and it’s just giving me strife at every turn. I do like that because it IS so pedantic, it’s a lot more stable, but I just wish it would ease up on some of the little things it pings you for, like spelling errors, missing semi-colons etc.

Anyways, that’s not why I’m posting today. I’m posting because I haven’t posted for a while, and because I haven’t posted for a while, I’m feeling generous. So here’s a couple of functions which you can use in your scripts, commercial, non-commercial, whatever, it’s all good. A shoutout on your site would be nice, but I’m not going to be pedantic about it… unlike PHP!

SMART SHORT

Shortens any given text to a specific length at the best possible place.

This is useful for blogs, news teasers, RSS feeds etc, the script is given a piece of text, a couple of end characters and an end range to find the best possible place to end the text, ie. end of a paragraph or sentence. This is better than just cutting the text at the Nth character, usually ending up with half-words or broken sentences.

<?php

function smart_short ($text, $end_chars = array(‘.’, ‘ ‘), $maxlength = 255, $slack = 50) {

    $maxlength2 = $maxlength + $slack;

    // If the text is shorter than the max length, just return the text

    if (strlen($text) <= $maxlength) {

        return $text;

    }

    // Check for suitable ending with an array of characters

    if (is_array($end_chars)) {

        foreach ($end_chars as $end_char) {

            $pos = strpos($text, $end_char, $maxlength);

            if ($pos <= $maxlength2 && $pos !== false) {

                return substr($text, 0, $pos + 1);

            }

        }

        // If it hasn’t returned by now, it means it couldn’t find anything to work with, so it

        // will try and find the characters before the maxlength.

        $sub_text = substr($text, 0, $maxlength);

        foreach ($end_chars as $end_char) {

            $pos = strrpos($text, $end_char, 0);

            if ($pos !== false) {

                return substr($sub_text, 0, $pos + 1);

            }

        }

        // If it still hasn’t returned by now, return the sub-text.

        return $sub_text;

    } else {

    // Check against one ending character

        $pos = strpos($text, $end_chars, $maxlength);

        if ($pos <= $maxlength2 && $pos !== false) {

            return substr($text, 0, $pos + 1);

        }

        $sub_text = substr($text, 0, $maxlength);

        $pos = strrpos($sub_text, $end_chars, 0);

        if ($pos !== false) {

            return substr($sub_text, 0, $pos + 1);

        }

        return $sub_text;

    }

}

?>

$text - The text to shorten

$end_chars - Characters which the script is allowed to end on. Can be a single character, or an array of characters.

$maxlength - The amount of characters you would like before the script finds a cutting point.

$slack - The amount of characters after the maxlength the scripts has to find a suitable end point.

FRIENDLY DATE

Converts a timestamp to a friendly date (ie. Today at 12:30pm - Yesterday at 7:30pm etc.)

<?php

function friendly_date ($date, $dateformat) {

    $seconds = time() - 60;

    $minutes = time() - 3600;

    $hours = mktime(0, 0, 0, date(‘n’), date(‘j’), date(‘Y’));

    $yesterday = mktime(0, 0, 0, date(‘n’), date(‘j’) - 1, date(‘Y’));

    if ($date > $seconds) {

        if (time() - $date == 1) {

            return ‘A second ago’;

        } else {

            return floor(time() - $date) . ‘ seconds ago’;

        }

    } elseif ($date > $minutes) {

        $temp_time = time() / 60;

        $temp_time_2 = $date / 60;

        $min_count = floor($temp_time - $temp_time_2);

        if ($min_count == 1) {

            return ‘A minute ago’;

        } else {

            return $min_count . ‘ minutes ago’;

        }

    } elseif ($date > $hours) {

        return ‘Today at ‘ . date(‘g:i a’, $date);

    } elseif ($date > $yesterday) {

        return ‘Yesterday at ‘ . date(‘g:i a’, $date);

    } else {

        return date($dateformat, $date);

    }

}

?>

$date - The timestamp

$dateformat - The date format the script will use, should it not be able to make the date friendly. See the date() PHP function.

That’s it for now, but there will be more to come. :)

[ Tags: , | No comments ]