inicio mail me! sindicaci;ón

Held Together by Duct Tape and Chewing Gum

Undeniable proof that the internet is held together by duct tape and chewing gum. No wonder wireless carriers need a 6500% markup on text messaging. Quoted from chess.com:

A few years ago I pulled a late-night session trying to debug a server at PacBell. The code looked fine, but every night the server would reboot and it was my job to figure out why. As I’m sitting there, testing the server at 1AM, in walks the janitor, UNPLUGS THE SERVER to plug in the vacuum, vacuums the room, then plugs the server back in. Case closed, I went home.

Find previous occurrence of string using PHP’s strrev() and preg_match()

Today I wrote a class to iterate words in a string. One challenge was finding my way backwards in a string. Specifically, given a starting position inside the string, I wanted to find the previous “word” and return it. However, since this needs to work localized (not just a-z), and the definition of a “word” is configurable, it was no simple matter of looking back for the previous space character.

So here is what I came up with; a method that finds the next or previous word given a starting position in the string:

   /**
    * Abstracted method for finding the next/prev word. This method assumes that 
    * $pos is greater than zero and less than the length of $text (check before calling)
    *
    * @param string $text the string of text to find next/prev word in
    * @param int $pos the position of first character in current word
    * @param string $wordPattern the regex definition of a word without any matching parens
    * @param string $reverse looks backward instead of forward (finds last word in string)
    * @return mixed false if no more words or array( "the word matched with junk", "the word only")
    */
   private static function nextWordMatch($text, $pos, $wordPattern, $reverse = false) {
      // we get the substring of text, starting at the current position
      if( $reverse ) {
         // in this case, we look at everything before $pos; we reverse it so that
         // we can run a simple regex on it rather than trying to deal with craziness
         // of looking backwards in string
         $text = substr($text, 0, $pos-1);
      }
      else {
         // in this case, we look at everything after $pos
         $text = substr($text, $pos);
      }
 
      // we escape the preg character just in case
      // we add in two sets of match parens, one for the word and one for the whole match
      // when looking backwards, we need to look from the end rather than the start
      $wordPattern = str_replace('@', '\\@', $wordPattern);
      $pattern = "(({$wordPattern})".self::NON_WORD_CHARS.")";
      if( $reverse ) { $pattern = "@{$pattern}\$@"; }
      else { $pattern = "@^{$pattern}@"; }
 
      // perform the match now and figure out what to do with it
      preg_match($pattern, $text, $matches);
      if( count($matches) < 3 ) { // remember that the first match is the raw text, so we add one
         // we didn't find any words, so return false
         return false;
      }
 
      // strip off the raw text, leaving our two matches
      return array_slice($matches, 1);
   }

Here is the default value for $wordPattern and the constant NON_WORD_CHARS used in the example:

   private $wordPattern = '\b[\w]+(?:[-\']\w+)*\b';
   const NON_WORD_CHARS = '\W*';

Photoshop Tutorials, Effects, Brushes, and more

A great Photoshop site with free brushes, effects, actions, and tutorials for the graphics enthusiast. Check it out: Photoshop Tutorials

Killing Users With Kindness

Coding Horror put up a great blog post on dialogs:

every time you send your users to an alert dialog, you have failed them.

Go check it out if you have a minute. I feel as strongly about this as Mr. Atwood.

Adding Nav Links and Sidebars in MediaWiki

Tried to edit my nav links on a new MediaWiki page today. Yikes.

It’s not exactly click-to-edit, but it’s not impossible either, once you’ve waded through the mis-information available about how this is done.

  1. 1. Type MediaWiki:Sidebar into the search box
  2. 2. Click Go
  3. 3. Click on the edit tab. If this page has never been visited, there may not be an edit tab; Click on create instead.
  4. 4. Make changes as desired.

Editing Other Bars

To edit other components on the sidebars, try the following pages:

Search Sidebar: MediaWiki:Search

Toolbox Sidebar: MediaWiki:Toolbox

Very Cheap, Really Nice Graphics

It takes a bit of surfing, but you can find some really nice graphics on this site at a fraction of the usual cost: http://graphicriver.net/category/graphics/backgrounds/nature

Great Compilation of CSS Techniques

This compilation is awesome, covering things like fluid horizontal and vertical layouts, sprites, and many other excellent techniques for CSS: http://www.smashingmagazine.com/2009/07/20/50-new-css-techniques-for-your-next-web-design/

Mind Mapping Software: Mindomo

Mindomo is a great, free product for mind mapping. You can utilize it online for free to create some great looking and functional maps.

There is also a pro version at the great price of $6/month, which provides a desktop version, allowing you to store the files locally and organize with folders and sharing capabilities.

With the upcoming addition of collaboraiton, and there continual efforts to improve the product, it’s quickly becoming a great brainstorming and outlining tool.

How to Run a Cygwin Command From Windows Scheduler (Scheduled Tasks)

After repeated problems setting up crond to run in cygwin (it just doesn’t like the user accounts, no matter how enthusiastically I argue that I’m me), I spent some time figuring out how to run a Cygwin command as a scheduled task from Windows.

Based on this mail archive post, I created the following cygrun.bat file:

    @echo off
    rem set HOME=c:\
    if "%DEF_PATH%"=="" set DEF_PATH=%PATH%
    set PATH=c:\cygwin\bin;%DEF_PATH%
    set myargs=%*
    if "%myargs%" == "" goto noarg
    rem echo %myargs%
    bash --rcfile "%HOME%/.bashrc" -i -c "%myargs%"
    c:
    rem pause
    sleep 1
    goto exit
    :noarg
 
    rxvt -e /usr/bin/bash --login -i
 
    :exit
    exit

Then I tested the script from the command line as follows, until I had the syntax just so:

c:
cd \cygwin
cygrun.bat cygwin_script arg1 arg2

Once I was able to run it happily, I added the scheduled task as follows:

    Run: C:\WINDOWS\system32\CMD.EXE /x /c start "Some title" /min
c:\cygwin\cygrun.bat cygwin_script arg1 arg2
    Start In: c:\cygwin     &lt;-- must be a real disk drive and path
    Run as: domain\username

Unfortunately, I was never able to figure out how to redirect stdout and stderr. I tried plenty of variations on “>> /some/path/to/log.txt 2>&1″ with no joy. Instead, I just changed all the commands in the script and added that line on to each echo statement. Sad, but functional.

Teaching Cron to Run Every Ten Minutes

I got tired of hacking cron and typing in ridiculous things like 0,5,10,15,20,25,30,35,40,45,50,55 to tell my cron jobs to run every five minutes.

Knowing that if I was annoyed by it, someone else with more time had probably suffered too, I hit Google. The result was surprisingly easy…
Read the rest of this entry »

« Previous entries · Next entries »