Program & Design Tips, tricks, tutorials, and tools on programming & web design

2Jun/090

Flash Player 10 on Ubuntu 64-bit

There's a wad of tutorials out there on how to to install this plugin, but I've found they either don't work, or over complicate things.

If you haven't already found it, download the Flash Player 10 64-bit plugin for linux. Then,

tar xvzf libflashplayer-10.0.22.87.linux-x86_64.so
sudo cp libflashplayer.so /usr/lib/firefox-3.0.10/plugins

You might be running a different version of Firefox by the time you read this tutorial, so bust a "locate firefox | grep plugins" to find the appropriate folder.

Filed under: Ubuntu No Comments
8May/090

Hash/unhash HTML

Here's a little class I wrote that lets you hash HTML and other things so that you can do some processing on just the text, and then unhash the HTML again.

class ht
{
        static $hashes = array();

        # hashes everything that matches $pattern and saves matches for later unhashing
       function hash($text, $pattern) {
                return preg_replace_callback($pattern, array(self,'push'), $text);
        }

        # hashes all html tags and saves them
       function hash_html($html) {
                return self::hash($html, '`<[^>]+>`');
        }

        # hashes and saves $value, returns key
       function push($value) {
                if(is_array($value)) $value = $value[0];
                static $i = 0;
                $key = "\x05".++$i."\x06";
                self::$hashes[$key] = $value;
                return $key;
        }

        # unhashes all saved values found in $text
       function unhash($text) {
                return str_replace(array_keys(self::$hashes), self::$hashes, $text);
        }

        function get($key) {
                return self::$hashes[$key];
        }

        function clear() {
                self::$hashes = array();
        }
}
Filed under: PHP No Comments
8May/090

PHP string ends with

There's a hundred ways to do this, but here's the one I came up with. It doesn't use regex's so it should be pretty quick.

function ends_with($str, $suffix) {
    return substr($str, -strlen($suffix)) == $suffix;
}

function starts_with($str, $prefix) {
    return substr($str, 0, strlen($prefix)) == $prefix;
}

For completeness, I added the corresponding starts_with function.

Filed under: PHP No Comments
8May/090

Random String

Just some functions for generating random strings in PHP.

function randstr($len=8, $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') {
    $i = 0;
    $str = '';
    $randmax = strlen($chars)-1;

    for($i=0; $i<$len; ++$i) {
        $str .= $chars[mt_rand(0,$randmax)];
    }

    return $str;
}

function randkey($len=32) {
    return randstr($len, '`1234567890-=qwertyuiop[]\\asdfghjkl;\'zxcvbnm,./ ~!@#$%^&*()_+{}|:"<>?');
}

randkey is good for generating salt strings and such, randstr is good for stuff like short URLs. Of course, you can pass in whatever characters you want.

Filed under: PHP No Comments
8May/090

Encode Array as String

I wanted to store an array in a cookie, so I wrote these two functions:

function encode_arr($data) {
    return base64_encode(serialize($data));
}

function decode_arr($data) {
    return unserialize(base64_decode($data));
}
Filed under: PHP No Comments
8May/093

jQuery Select Text Range

Here's a jQuery function I wrote which you can use to select a range of text in an input field.

$.fn.selectRange = function(start, end) {
    return this.each(function() {
        if(this.setSelectionRange) {
            this.focus();
            this.setSelectionRange(start, end);
        } else if(this.createTextRange) {
            var range = this.createTextRange();
            range.collapse(true);
            range.moveEnd('character', end);
            range.moveStart('character', start);
            range.select();
        }
    });
};
Tagged as: 3 Comments
26Apr/090

Simple Rounded Corners with CSS

I know there are umpteen billion tutorials on rounded corners out there, but here's an easy way that I like to do it. Requires only one image, and allows you to nudge your text as close to the corners are you like.

If you want a 3px radius corner, have a black background, and white foreground, you need to create a 6x6 (i.e. radius*2) image of a white circle on a black background. This method won't support transparency unfortunately.

The HTML:

<div id="content">
    <div id="tl"></div><div id="tr"></div><div id="bl"></div><div id="br"></div>
    Your content here.
</div>

The CSS:

#content {
    position:relative;
    background-color: #fff;
    width:600px;
    height:300px;
}

#tl,#tr,#bl,#br{
    background: #000 url(images/circle.gif);
    width:3px;
    height:3px;
    position:absolute;
}

#tl{top:0;left:0}
#tr{top:0;right:0;background-position:3px 0}
#bl{bottom:0;left:0;background-position:0 3px}
#br{bottom:0;right:0;background-position:3px 3px}

Adjust all the "3px"s to match your radius, and modify the background attribute to meet your needs, and style content however you want. You probably want to put some padding on there. The important thing is that #content has position:relative so that the corners are aligned to the content box rather than the page.

I used this technique on my personal site, mnbayazit.com.

Filed under: Uncategorized No Comments
26Apr/090

Vertically center text with CSS

Anyone who has used CSS for awhile knows that vertically aligning stuff isn't easy. Vertically aligning just text, however, is pretty simple. If your container is 30px tall, just set the line-height to 30px too and your text will be perfectly centered! Hurray! This only works for a single line of text however... if you want more, you're probably better off fighting with margins, padding, or absolute positioning.

Filed under: Java No Comments
14Apr/090

Draw an Unfilled Circle

OpenGL doesn't seem to have any functions for drawing an unfilled circle, so you can use this code instead. It uses lines, so you can adjust the line thickness with glLineWidth and anti-alias it with glEnable(GL_LINE_SMOOTH) if you want.

void drawCircle(GLfloat x, GLfloat y, GLfloat r) {
    static const double inc = M_PI / 12;
    static const double max = 2 * M_PI;
    glBegin(GL_LINE_LOOP);
    for(double d = 0; d < max; d += inc) {
        glVertex2f(cos(d) * r + x, sin(d) * r + y);
    }
    glEnd();
}

If you want a filled circle, I would suggest you just set glPointSize to whatever size you want, and then plot a single point with glBegin(GL_POINTS) where ever you want your circle. Otherwise, you can use the above code with GL_POLYGON if you want more control. If you want a smoother circle, decrease the size of "inc".

Tagged as: No Comments
14Apr/090

Base62 Encode

If you have large integers and you want to shrink them down in size for whatever reason, you can use this code. Should be easy enough to extend if you want even higher bases (just add a few more chars and increase the base).

function encode($val, $base=62, $chars='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') {
    // can't handle numbers larger than 2^31-1 = 2147483647
    $str = '';
    do {
        $i = $val % $base;
        $str = $chars[$i] . $str;
        $val = ($val - $i) / $base;
    } while($val > 0);
    return $str;
}

function decode($str, $base=62, $chars='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') {
    $len = strlen($str);
    $val = 0;
    $arr = array_flip(str_split($chars));
    for($i = 0; $i < $len; ++$i) {
        $val += $arr[$str[$i]] * pow($base, $len-$i-1);
    }
    return $val;
}

echo encode(2147483647); // outputs 2lkCB1
Filed under: PHP No Comments