Posts tagged Php

August 22th, 2011

bit-ratchet, easier binary parsing in PHP

Written by Dave BarkerTopics: Php, Project, Code

Recently I needed to write some code to parse a binary protocol in PHP. Given a ASCII hex string representation of the message I needed to pull off bits and bytes, using them in lots of different ways.

Problem was that you have to read a byte at a time from the hex string, then manually shift bits each time to get what you want. This gets old and confusing very quickly! To solve this I've written bit-ratchet, a small class that lets you read bits and bytes from a hex string very simply.

The idea is you create a bit-ratchet object of your hex string. You then ask for bits and bytes, signed or not and bit-ratchet provides them whilst keeping track of your current position in the data. Additionally I added a few useful methods allowing you to skip, jump and pull off hex.

Things like scaling are left to the user, the one exception being signed numbers which are handled by bit-ratchet. I wanted to avoid creating a complex library like bindata, in my opinion although it's great the DSL it provides is constraining. Better to let the user use the host language and as far as possible stay out of the way!

It scratched my itch but let me know if you've got any ideas about how to expand on the abstraction.

Cheers, Dave

October 01th, 2010

The Good JuJu List Machine

Written by Dave BarkerTopics: Php, Project, News

I have just released the first version of The Good JuJu List Machine. This is a viral list building application that's similar to Frank Kern's Good Karma List Machine.

If your not familiar with the idea basically it's a tool to help increase opt-ins to your mailing list virally. It offers free content as bait to get people to subscribe to your mailing list. Then when the subscriber confirms their email address they receive their prize and are also encouraged to share the link with their friends to receive even more free content.

I have listened to the people on the warrior forums and have implemented mostly everything that's been asked for, I know there where sticking points with a lot of the alternatives.

Features

  • Wordpress plugin, written for version 3.0.1
  • Supports any list provider like Aweber that can forward confirmed opt-ins to a "thank you" page
  • Doesn't require password or anything else that could confuse or annoy potential subscribers.
  • Free + opensource, I've coded it from scratch and released under the GPL.
  • Completely customisable HTML for each page.
  • Link reminder feature helps keep everyone happy.

Opt-in Process

The script works slightly differently to the alternatives, I've re-designed the process to reduce confusion and annoyance. First off I removed passwords, replacing them with a "private page link". I did this because we're trying to increase opt-ins not protect access to the bait content. Second I've re-structured things to allow integration with pretty much any list provider.

The opt-in process

Download

Demo / Screenshots

I developed the script on a new website I'm working on, the site isn't finished but you can give the JuJu list machine a test. Just opt-in at the top right and you will see how it all works. Also here's a quick screenshot , I'll add some more later.

Setup Instructions

Setting this thing up is easy:

  1. Firstly download the script and extract the archive into your wp-content/plugins/ directory.
  2. Now load your Wordpress admin, click 'Plugins' and then click 'Activate' below the 'Good JuJu List Machine' plugin.
  3. Now click 'Good JuJu List Machine' under the Plguins menu on the left hand side.
  4. OK, now follow the instructions on the JuJu page. (There's not much to do, just customise the templates, set 3 preferences and configure your email list.

Template codes

There are a few codes you can use in your templates to help insert the dynamic elements:

  • %private-link% - The user's private page link.
  • %referal-link% - The user's referral link.
  • %referals-sofar% - How many referrals the user has racked up so far
  • %basic-link% - Link to the free content.
  • %extra-link% - Link to the extra free content given for the referrals.
  • %email% - The user's email address.
  • %content-links% - The links to the content, it includes either the basic links or extra links depending on their referals.
  • %lost-link% - A link to the "I've lost my link" page.
  • %outside-link% - A link to the front page of the script.
  • %referals-needed% - The number of referrals needed for the extra bonus content.

Cheers, Dave

Edit: This didn't go down too well, the warrior forum guys just deleted my post and no one even tried it. The code isn't too nice, it was a very quick initial version I wanted to get out there to get feedback. It's not bad but it's got plenty of room for improvement, who knows maybe it will take off sometime. It definitely taught me that I don't want to work with the warrior forum crowd anyway.

March 04th, 2010

Round image corners with php

Written by Dave BarkerTopics: Howto, Php, Code

I needed to round the corners of a banner image dynamically in PHP and I couldn’t find any explanations. Anyway it turns out the easiest way is to make an image of a rounded corner, like this rounded-corner.png and then stamp it on all 4 corners. You have to rotate it each time of course.Anyway here’s a snipplet of code to give you an idea:

<?php
$banner = '/path/to/banner-image.png';
$corner = '/path/to/rounded-corner.png';

// Load up the images
$banner = open_image($banner);
$corner = open_image($corner);

// Round the corner
imagecopy($banner, $corner, 0, 0, 0, 0, imagesx($corner), imagesy($corner));
$corner = imagerotate($corner, 90, 0);
imagecopy($banner, $corner, 0, imagesy($banner) - imagesy($corner), 0, 0, imagesx($corner), imagesy($corner));
$corner = imagerotate($corner, 90, 0);
imagecopy($banner, $corner, imagesx($banner) - imagesy($corner), imagesy($banner) - imagesy($corner), 0, 0, imagesx($corner), imagesy($corner));
$corner = imagerotate($corner, 90, 0);
imagecopy($banner, $corner, imagesx($banner) - imagesy($corner), 0, 0, 0, imagesx($corner), imagesy($corner));

// Output the image
header("Content-type: image/gif");
imagegif($banner);

// Tidy up
imagedestroy($banner);
imagedestroy($corner);
?>

December 17th, 2008

Quick stripping non-ascii in PHP

Written by Dave BarkerTopics: Code, Php

Working on this contract required me to strip non-ascii characters out of their content before sending it to Sphinx to be indexed. “No problem” I thought, “I’ll just use that function I wrote earlier“…

Whoops, to filter some test data it took like 3 minutes! So I did some further tests, here where my findings:

  • strip_utf - 3 mins and 20.546s
  • str_replace($crap, ”, $string) - 3 mins 6 seconds
  • preg_replace(’/[^(\x20-\x7F)]*/’, ”, $content) - 2 mins 10 seconds
  • No filtering - 8 seconds!

So it still wasn’t fast enough. Finally I wrote a small chunk of C to strip the crap, piping the data through that gave:

  • 12 seconds

Not bad! The code is pretty simple too, in case you want it here you go:

Wordpress has mangled the code so badly, I have given up trying to get it to display properly. View it here

Edit: Wordpress' gone so here you go:

#include <stdio.h>
#include <stdlib.h>

int main(void) {
  unsigned int Chr;

  while ((Chr = getchar()) != EOF) {
    if ((Chr > 31 && Chr < 128) || (Chr == 13) || (Chr == 10))
      putchar(Chr);
  }
  return 0;
}

Next up, making a module to use some C to strip these characters from within PHP..

(Also next Wordpress is going to be replaced)

- Dave.

(Thanks to erisco in ##php for the suggestions and help.)

Edit: By the way the time of 3 minutes mattered because that meant with the entire database the time would be like 5 or 6 hours, with the speed increase I managed to get it’s down to 20 minutes :D

December 03th, 2008

Parsing a number from a string in PHP

Written by Dave BarkerTopics: Php, Code, Howto

Another quick PHP snippet, a function that returns the first integer found in the string given:

// Take a string and return the first number found
function parse_int($string) {
  ereg("([0-9]+)", $string, $results);
  if (is_array($results))
    return (int)$results[0];
  else
    return NULL;
}

December 02th, 2008

How to strip Unicode and UTF using PHP

Written by Dave BarkerTopics: Code, Php, Howto

I have been working on a PHP cotract recently and I needed to strip out anything non-asci.

Here’s a little function I wrote that takes a string and returns it minus all the crazy symbols:

// Function to strip out all non asci characters
function strip_utf($string) {
  for ($i = 0; $i < strlen ($string); $i++)
    if (ord($string[$i]) > 127)
      $string[$i] = " ";
  return $string;
}

Hope it helps, Dave.