Archive for the ·

php

· Category...

don’t let the regex slash get you down

no comments

This is quick, but important for anyone who has to deal with regular expressions occasionally.

Sometimes I see regular expression search-and-replace lines that look like this, often from developers I consider far more advanced than me.

/\/\/root\/\/path\/node/\/\/root2\/\/path\/node

Remember: in nearly all regex implementations, you can swap out the slash for any one-character keyword.  If you’re working with URLs and file paths, this is a must if you want to avoid too much slash escaping.

Compare this to the previous expression:

s#//root//path/node#//root2//path/node

Much more readable!  Whichever character you choose (I like # because I rarely use it, but : is an easy-to-type option), just place it right after the ‘s’.  Use the same character between the expression search and result, and at the end if you’re adding optional flags.  This works in most programming & scripting languages, and tools like sed.

Open files from the command line in PHPStorm

1 comment

I’m a big terminal fan, and I’m also a PHP developer.  Last month, I switched from Netbeans to PHPStorm for a full-fledged PHP IDE1.  Often, I’m in the terminal, and find it natural to open a file in PHPStorm from there.  Awkwardly, you need to use the full path of the file to do this.  That means, if I’m in /var/www/some_client/some_site/htdocs/resources/web/ I have to type this command just to open index.php in that directory:

phpstorm /var/www/some_client/some_site/htdocs/resources/web/index.php

Ugh. Far too much typing. I looked for a shell integration plugin, or a setting under File -> Settings, where PHP Storm crams thousands of options.2

It turns out, I was just looking in the wrong place. You have to set up this feature, but it’s dead simple. Open Tools -> Create Command-Line Launcher….

PHP Storm Command Line setup menu option

Next, pick a name for the executable and a path to place the shortcut in.  The name is the command you’ll type to run phpstorm from the terminal, so you can make it shorter for even less typing!  The path should be in your system shell’s $PATH variable; PHPStorm’s default is probably what you want if you don’t know what this means.

PHP Storm Command Line Config dialog

PHP Storm Command Line Config dialog

Hit OK to save.  If your user doesn’t have acces to that Path, you may be prompted to enter a password for the system root user.

Once you’re done, you can navigate into any directory and run your command to open files directly in PHP Storm.

pstorm somefile.php

Your life will be 30% better from now on.

  1. mostly on account of IdeaVIM’s excellent VIM emulation.  It’s not complete, but netbeans’ version is vastly inferior.
  2. For a while, I resorted to a slightly shorter workaround, typing “phpstorm `pwd`/index.php” in these situations… still too much typing