Entrepreneur Geek

Nirav Mehta on life, technology and future

Archive for November, 2008

40 Tips for Optimizing PHP Performance

without comments

Reinhold Weber has written 40 Tips for Optimizing PHP performance. Many of them were new to me – e.g. $row['id'] is 7 times faster than $row[id]. I don’t know how he has measured these numbers, but the tips are good overall!

Here are the first 10 tips:

  1. If a method can be static, declare it static. Speed improvement is by a factor of 4.
  2. echo is faster than print.
  3. Use echo’s multiple parameters instead of string concatenation.
  4. Set the maxvalue for your for-loops before and not in the loop.
  5. Unset your variables to free memory, especially large arrays.
  6. Avoid magic like __get, __set, __autoload
  7. require_once() is expensive
  8. Use full paths in includes and requires, less time spent on resolving the OS paths.
  9. If you need to find out the time when the script started executing, $_SERVER[’REQUEST_TIME’] is preferred to time()
  10. See if you can use strncasecmp, strpbrk and stripos instead of regex

Read the full list of 40 tips here.

Read comments on the post as well, they too contain useful information.

Written by Nirav

November 28th, 2008 at 3:01 pm

Posted in PHP

Tagged with ,

Opening External Links in new window from AS3

with 9 comments

You most probably know that there are issues opening links using the navigateToURL() method in Flex. The three most common problems / issues are:

  • allowScriptAccess not set to all in the HTML Wrapper
  • Window Mode set to transparent in IE
  • Popup blockers blocking the new window – specially when target is _blank

Many people have tried solving this issue. My approach is simply a hack over FlexMerge’s code. Which in turn is a hack on “Jason the SAJ”’s approach. Another approach I liked in detecting if popup blockers blocked your window is Andrew Trice’s solution on InsideRIA.

Essentially, use JavaScript window.open method for Firefox, a little tweak on that for IE and use navigateToURL() for other browsers.

Continue reading for the code…
Read the rest of this entry »

Written by Nirav

November 27th, 2008 at 11:58 am

Introduction to Screencasting in Linux – recordMyDesktop

without comments

Screencasting is a great way to share information. I use it to show bugs to my team, to record training videos of the project management system I am working on, and even to record demos of project deliveries to clients. I use Jing to record videos from my Mac. But wanted to find something useful for Linux.

recordMyDesktop is a great Linux tool to record the entire desktop, a portion or an application. It outputs Ogg format video, which can easily be converted to AVI, QuickTime or FLV. It’s been there for a while, and is pretty solid.

What sold me though, was this video explaining how to use recordMyDesktop – and then to convert saved video into other formats. It’s easy to follow and covers all important aspects.

Introduction to Linux Screencasting - using recordMyDesktop

Introduction to Linux Screencasting - using recordMyDesktop

If you want to record screens from Linux, this is the best tool. Try it out!

(thanks Santosh for pointing me to this!)

Written by Nirav

November 24th, 2008 at 11:40 am

Posted in GNU/Linux

Tagged with ,

How to stop keyboard event propagation – Flex / JavaScript?

with 6 comments

In a Flex application we are building, we want to listen to Ctrl+N to create a new file. The browser listens to the keystroke and opens a new window. We do not want it to open a new window.

This has been a problematic thing for many. I did a lot of research, trial & error, but I still don’t have a solution. I can catch all key events, using some JavaScript, I can also prevent Ctrl+W and Ctrl+S. But not Ctrl+N. Do you have something?

References:

Situations / Test Cases:

  • Should work: Ctrl + N (New), Ctrl + O (Open), Ctrl + W (Close), Ctrl + Q (Quit)
  • Ctrl + C (Copy), Ctrl + V (Paste), Ctrl + X (Cut) – this works with current Flex app. New code must not break it
  • Should work on Windows, Mac, Linux
  • Should work on Firefox, IE, Safari, Chrome
  • What if focus is not in Flex app?
  • What if user goes to another window, comes to our app and presses a keyboard shortcut? (this will lose focus)
  • Will cut / copy / paste from Browser’s Edit menu work?

JavaScript does it:

Flex documentation says:
When handling a key or key combination that the underlying operating system or browser recognizes, the operating system or browser generally processes the event first. For example, in Microsoft Internet Explorer, pressing Control+w closes the browser window. If you trap that combination in your Flex application, Internet Explorer users never know it, because the browser closes before the ActiveX Flash Player has a chance to react to the event.

Notes:

  • KeyboardEvent is non cancelable in Flex.
  • stopImmediatePropagation(), preventDefault(), stopPropagation() – don’t stop the propagation
  • I can detect the keys very well, problem is suppressing the default browser action
  • Don’t want to have a full HTML/JavaScript/ExternalInterface handling like Buzzword. Ideally, want to let Flex handle it’s own events, HTML handle browser events and suppress them.

Any ideas?

TIA!

Written by Nirav

November 19th, 2008 at 5:55 pm

Removing Word special characters from text / XML in PHP

without comments

Microsoft Word converts certain characters into "smart characters". Double quotes, dashes (em dash / en dash), bullets and so on.

These characters break PHP's XML handling. (or at least they broke it for me - using simplexml_load_string!).

How do you clean them?

There is an old post that suggests using ereg_replace on a set of characters - essentially converts them to html entities.

Unfortunately, that did not work with me. Since the text is UTF8, the replace logic replaced alphabets too.

I tried a lot to get a solution, but could not find something that would work. Finally, just stripped out all non printing characters except line breaks and tabs.

PHP:
  1. private function cleanWordSpecialCharacters($body)
  2. {
  3.   $body = preg_replace( '/[^[:print:]|\n|\r|\t]/', '', $body );
  4.   return $body;
  5. }

This too breaks with non English characters. Any suggestions?

Written by Nirav

November 15th, 2008 at 5:58 pm

Posted in PHP

Tagged with , ,