lime icon

Phosphorus and Lime

A Developer's Broadsheet

This blog has been deprecated. Please visit my new blog at klenwell.com/press.
PHP str_to_word Challenge
This is the challenge I faced: tokenize a string into words. Easy enough you say, just use one of the many built-in PHP functions, like str_count_words(). But now, do it successfully with a string like this:

I 'said', "This is my test's string of over '8.8' words.About 16 to be ex-act!"


I think I've found the solution:

function str_to_words($str)
{
$re = '%[^A-Za-z0-9\-\'\.]+|([A-Za-z]+)\.([A-Za-z]+)%';
$CHR = str_split('abcdefghijklmnopqrstuvwxyz0123456789');
$W = preg_split($re, $str, -1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE);

// make sure first and last characters are alphanumeric
// do we sacrifice ' in plural possessive (e.g. the girls' toys)? no
foreach ( $W as $w0 )
{
$w1 = ( !in_array(strtolower(substr($w0,0,1)),$CHR) ) ? substr($w0,1) : $w0;
$w1 = ( !in_array(strtolower(substr($w1,-1)),$CHR + array("'")) ) ? substr($w1,0,-1) : $w1;
$WORDS[] = $w1;
}

return $WORDS;
}


Result:

Array
(
[0] => I
[1] => said
[2] => This
[3] => is
[4] => my
[5] => test's
[6] => string
[7] => of
[8] => over
[9] => 8.8
[10] => words
[11] => About
[12] => 16
[13] => to
[14] => be
[15] => ex-act
)


Of course, there are a few fringe cases it won't. And bear in mind that it's about 200x slower than str_count_words(). Fortunately, it's still fast enough for my purposes.

Labels:

Komodo Edit Macros
Well, as soon as I had settled upon gvim as my new editor de riguer, a friend at work pointed me to Komodo Edit. Now this is what I was looking for. What a weight off my mind.

Never been a big macro-head, but there's a couple things I wanted to do that you can't do through the preferences menu alone. So I figured out a couple tricks and wrote the following macros. (What is cool is that you can use either javascript or python for the macros. Sweet!)

add html comment
// comment_html: <!-- {|} -->
// author: klenwell@gmail.com
komodo.assertMacroVersion(2);

var addLeft = '<!-- ';
var addRight = ' -->';
var currentPos = komodo.editor.currentPos;
var selText = komodo.editor.selText;
var newText = addLeft + selText + addRight;
var selTextPos = currentPos - selText.length + newText.length;
var noSelTextPos = currentPos + newText.length - addRight.length;
var newPos = ( selText != '' ) ? selTextPos : noSelTextPos;


if (komodo.view.scintilla) { komodo.view.scintilla.focus(); }
//if (komodo.view) { komodo.view.setFocus() };

komodo.view.selection = newText;
komodo.editor.gotoPos(newPos);
komodo.view.selection = '';


this will wrap any select text like so: <!-- {select text} --> or add an empty wrapper with the cursor in the middle: <!-- | -->

The same principle can be applied to many situations:

echo php inline
// echo_php_inline: <?php echo {|} ?>
// author: klenwell@gmail.com
komodo.assertMacroVersion(2);

var pcmd = 'echo'; // echo or print
var addLeft = '<?php '+pcmd+' ';
var addRight = '; ?>';
var currentPos = komodo.editor.currentPos;
var selText = komodo.editor.selText;
var newText = addLeft + selText + addRight;
var selTextPos = currentPos - selText.length + newText.length -3;
var noSelTextPos = currentPos + newText.length - addRight.length;
var newPos = ( selText != '' ) ? selTextPos : noSelTextPos;


if (komodo.view.scintilla) { komodo.view.scintilla.focus(); }
//if (komodo.view) { komodo.view.setFocus() };

komodo.view.selection = newText;
komodo.editor.gotoPos(newPos);
komodo.view.selection = '';


Add a key-binding through the properties menu and you're a rockstar.
Cutting, Copying, and Pasting in Vim
Got a new laptop a couple weeks ago and finally jumped in the deep-end of the Linux pool. Probably the most painful part of the transition has been abandoning my reliable old html-kit editor and trying to find a new one. After a few dead-ends, I finally bit the bullet and committed to Vim (gVim to be exact). For the first few hours, I was ready to cry. But after doing some research on key-mappings, I'm starting to get the hang of it.

The biggest frustration was trying to break my Pavlovian conditioning to use Ctrl-X,-C,-V. Of course, once I figured out mapping, I could remap the keys. It is not recommended to remap Ctrl-C, so I compromised and use y like you're supposed to.

Here's the relevant portion of my .gvimrc file:

" --- KEY MAPPINGS --- "
" Visual Cut/Copy/Paste : http://www.vim.org/tips/tip.php?tip_id=866
" Cut -- using register '+' [Ctrl-o -> jump to last pos]
map "+d
vmap "+d
imap "+d
" Copy -- yank to register '+' [not needed for imode]
noremap y "+y
vnoremap y "+y
" Copy All
imap gggHG
vmap gggHGi
" Select Line
map 0v$
vmap 0v$
imap 0v$
" Paste
map "+gP
vmap "+gP
imap "+gP


Still miss html-kit, but I'm adapting.

Labels: