Maarten’s Posterous

Internet Development 

Leading zeros in PHP

This is how you add a certain number of leading zeros to integers in PHP.

$x = 1;
$y = sprintf("%03d",$x);
echo $y;

The 3 in "%03d" is the amount of digits you have in the end. So %02d if you only want 2 digits.

Source: Romeo, http://www.webmasterworld.com/forum88/4273.htm

Comments [0]

Minimizing image hover flicker in Internet Explorer 6

Just paste this in your .htaccess file and the flicker is far better.

ExpiresActive On
ExpiresByType image/gif A2592000
ExpiresByType image/jpeg A2592000
ExpiresByType image/png A2592000

Source: http://www.fivesevensix.com/studies/ie6flicker/

Comments [0]

Redirect

This is how you do a 301 Redirect in PHP

<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.example.com/newlocation.html");
?>

This is how you do the same thing in .htaccess

Redirect 301 /oldlocation.html http://www.example.com/newlocation.html

Comments [0]

MySQL Database Filtering

Database filtering

Here are some queries I gathered off the Internet to filter your database when it's cluttered.

This is a way to check if your table has any duplicates in a specific field.
(src: http://mysql-tips.blogspot.com/2005/04/mysql-find-duplicate-repords-example.html)

    SELECT `email`, count(`email`) as cnt
  FROM registree
  GROUP by `email`
  HAVING cnt > 1
  ORDER BY cnt;

This is a way to remove duplicates in MySQL:
(src: http://mediakey.dk/~cc/mysql-remove-duplicate-entries/)

    ALTER IGNORE TABLE `registree` ADD UNIQUE INDEX(`email`);
   
This is how you convert fields to sentence case in MySQL. (Converts only the first letter of a field to uppercase).
(src: http://eisabainyo.net/weblog/2007/07/23/convert-to-sentence-case-in-mysql/)
   
  UPDATE `registree`
  SET `firstname` =  CONCAT(UCASE(SUBSTRING(`firstname`,1,1)),'',
  LCASE(SUBSTRING(`firstname`,2,LENGTH(`firstname`))))

   
This is how you convert fields to lower case in MySQL.
(src: http://www.tbforum.nl/thread/79221.html)

    UPDATE `registree` SET `email` = LOWER(`email`);

This is the ucfirst() equivalent for MySQL. (Converts the first letter of each word in a field to uppercase).
(src: http://forums.devshed.com/showthread.php?p=1922166#post1922166)

    DROP FUNCTION IF EXISTS proper;
  SET GLOBAL  log_bin_trust_function_creators=TRUE;
  DELIMITER |
  CREATE FUNCTION proper( str VARCHAR(128) )
  RETURNS VARCHAR(128)
  BEGIN
    DECLARE c CHAR(1);
    DECLARE s VARCHAR(128);
    DECLARE i INT DEFAULT 1;
    DECLARE bool INT DEFAULT 1;
    DECLARE punct CHAR(17) DEFAULT ' ()[]{},.-_!@;:?/';
    SET s = LCASE( str );
    WHILE i < LENGTH( str ) DO
      BEGIN
        SET c = SUBSTRING( s, i, 1 );
        IF LOCATE( c, punct ) > 0 THEN
          SET bool = 1;
        ELSEIF bool=1 THEN
          BEGIN
            IF c >= 'a' AND c <= 'z' THEN
              BEGIN
                SET s = CONCAT(LEFT(s,i-1),UCASE(c),SUBSTRING(s,i+1));
                SET bool = 0;
              END;
            ELSEIF c >= '0' AND c <= '9' THEN
              SET bool = 0;
            END IF;
          END;
        END IF;
        SET i = i+1;
      END;
    END WHILE;
    RETURN s;
  END;
  |
  DELIMITER ;
   
  UPDATE `registree` SET `email` = proper(`email`);

This last one is a little labourious... But it works! If this is a bit too difficult for you, take a look at PHP's ucfirst() over here http://www.php.net/manual/en/function.ucfirst.php, since there isn't much MySQL coverage on this topic.




Comments [0]

Force error reporting with .htaccess

This is a way to force error reporting in PHP via .htaccess. Thanks to THC-Rob (http://www.totalchoicehosting.com/forums/index.php?showtopic=19974).

php_flag display_errors on
php_value error_reporting 7

Very nice for testing purposes if your server has PHP error reporting disabled for security reasons.

Comments [0]

Function to detect if something is even or odd...

... for all your striper needs.

function is_odd( $int ){
    return( $int & 1 );
}

Comments [0]

Get filesize in PHP

Here's a function to get the size of a file on a server in PHP.

function get_filesize($url,$thereturn) {
    if (substr($url,0,4)=='http') {
        $x = array_change_key_case(get_headers($url, 1),CASE_LOWER);
        $x = $x['content-length'];
    } else {
        $x = @filesize($url);
    }
    if (!$thereturn) {
        return $x;
    } elseif($thereturn == 'mb') {
        return round($x / (1024*1024),2);
    } elseif($thereturn == 'kb') {
        return round($x / (1024),2);
    }
}

I'm not sure where I got this exactly, I'm guessting http://www.php.net. So credits to the original author.

Comments [0]

If JavaScript is disabled...

Apparently, if you want to display a message in case the user has no JavaScript support or if his JavaScript support is turned off, you can just use <noscript>Message</noscript>. Only if the user has no JavaScript enabled, he sees this message, which is pretty neat!

Comments [0]

Clean Up Magic Quotes

Here's a function to clean up magic quotes (extracted from the PHP manual).

if (get_magic_quotes_gpc()) {
    function stripslashes_deep($value){
        $value = is_array($value) ?
                    array_map('stripslashes_deep', $value) :
                    stripslashes($value);

        return $value;
    }
    $_POST = array_map('stripslashes_deep', $_POST);
    $_GET = array_map('stripslashes_deep', $_GET);
    $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
    $_REQUEST = array_map('stripslashes_deep', $_REQUEST);
}

Comments [0]

PHP Limit Words Function

This function will shorten a paragraph, but will not cut in the middle of a word...

function limit_words($string, $word_limit) {

    $words = explode(' ', $string, ($word_limit + 1));
    array_pop($words);
    return implode(' ', $words);
}

Comments [0]