For MySQL, utf8 is not enough, you want utf8mb4

Someone entered 😨, the “fearful face emoticon”, in a forum on a website I run. But it didn’t display.

Here’s what I had to do to get it to work:

  • ALTER DATABASE dbname CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci
  • ALTER TABLE tablename CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci
  • ALTER TABLE tablename MODIFY columnname COLUMNNAMEEXISTINGTYPE CHARACTER SET utf8mb4;
  • In my.conf:

    [client]
    default-character-set = utf8mb4

    [mysqld]
    collation-server = utf8mb4_unicode_ci
    init-connect=’SET NAMES utf8mb4′
    character-set-server = utf8mb4

Note that if you are converting from latin-1 not utf-8 you will need to convert columns to blob and then to utf8mb4 to correctly preserved already latin1-encoded special characters.

Convert from underscore_style to camelCaseStyle and Vice Versa

  // AStringConverted -> a_string_converted
  function toUnderscoreStyle($s) {
    $f = function ($match) { return "_" . mb_strtolower($match[1]); };
    return preg_replace_callback('/([A-Z])/', $f, lcfirst($s));
  }

  // a_string_converted -> aStringConverted
  function toCamelCase($s) {
    $f = function($match) { return mb_strtoupper($match{1}); };
    return preg_replace_callback('/_([a-z])/', $f, $s);
  }

Arguing on the Internet

wrong

“Don’t read the comments” is a pretty good warning. You can waste a lot of time shouting at strangers in comment threads or fighting with friends-of-friends on Facebook. But if you feel like you do have to engage I really like these rules from Anatol Rapoport by way of Daniel Dennett to give you a fighting chance of changing a mind rather than just blowing hot air.

How to compose a successful critical commentary:

  1. You should attempt to re-express your target’s position so clearly, vividly, and fairly that your target says, “Thanks, I wish I’d thought of putting it that way.
  2. You should list any points of agreement (especially if they are not matters of general or widespread agreement).
  3. You should mention anything you have learned from your target.
  4. Only then are you permitted to say so much as a word of rebuttal or criticism.

Remember the Milk Alfred Plugin that Respects Double Quotes, Single Quotes, Backslashes and Anything Else You Throw At It

I’ve used various Remember the Milk plugins for Alfred but none of them cope with any input and reproduce it faithfully in the resulting task. Here’s a plugin that does. You need Alfred’s Power Pack to use this.

$ sudo gem install rumember
$ ru # Agree to authenticate rumember, then press Enter in the terminal when authenticated

Now install this plugin. It’ll use “todo” as the keyword by default but that’s easily changed under ‘Workflows’ in Alfred’s settings should you wish.

Things I Absolutely Must Do On A New Mac

I try to stick to “default is better than configuration”. But there’s some stuff I absolutely must have on a Mac to be useful.

A Very Short But Complete Guide to Sending a Pull Request to a github Project

  1. Clone the project
          $ git clone https://github.com/OWNER/REPO.git
    

    You may need to fork the project and clone your own forked version.

  2. Make a branch and switch to it
          $ git checkout -b YOURBRANCH
    
  3. Make your changes
  4. Add your changes to the index
          $ git add -p
    
  5. Commit your changes
          $ git commit -m "My commit message"
    
  6. Push your changes
          $ git push --set-upstream origin YOURBRANCH
    
  7. Visit https://github.com/YOURUSERNAME/REPO/tree/YOURBRANCH
  8. Click on New Pull Request
  9. Write something sensible
  10. Click on Send Pull Request

Grade Between Two Hex Colors

CSS colors gradation example

Yesterday I wanted to gradually shade between two hex colors. To perform artihmetic/math on hex colors in PHP you need to break them apart, turn them into decimals, change them back into hex and then put them back together.

Here’s what I came up with:

function hexColorToRgb($color) {
    return [hexdec(mb_substr($color, 0, 2)),
        hexdec(mb_substr($color, 2, 2)),
        hexdec(mb_substr($color, 4, 2))];
}

function colorGrade($p, $start, $end) {
    $start_rgb = hexColorToRgb($start);
    $end_rgb = hexColorToRgb($end);
    $rgb = [$start_rgb[0] * (1.0 - $p) + $end_rgb[0] * $p,
            $start_rgb[1] * (1.0 - $p) + $end_rgb[1] * $p,
            $start_rgb[2] * (1.0 - $p) + $end_rgb[2] * $p];
    return dechex($rgb[0]) . dechex($rgb[1]) . dechex($rgb[2]);
}

// Example:
$start_color = 'c4dbf5';
$end_color = '333333';
echo colorGrade(0.25, $start_color, $end_color); # 9fb1c4
echo colorGrade(0.5, $start_color, $end_color);  # 7b8794
echo colorGrade(0.75, $start_color, $end_color); # 575d63

PHP Lint in Makefile

I ran into a little problem adding php -l to a Makefile. PHP lint is a little verbose outputting “No syntax errors detected” for every file that has no problems. I don’t really want that output but getting rid of it with grep -v means that make treats no errors (grep didn’t find any lines to show) as a failure and errors (grep found some lines to show) as a success. make then terminates in the success case and continues or exits with success in the failure case!

lint:
	@! find . -name "*.php" | grep -v "^./vendor" | xargs -I{} php -l '{}' | grep -v "No syntax errors detected"

The trick is to negate the exit code of the entire pipeline like so:

lint:
	@! find . -name "*.php" | grep -v "^./vendor" | xargs -I{} php -l '{}' | grep -v "No syntax errors detected"