Spooky Case of the Wem Ghost
Installing Proprietary nVidia Drivers on Ubuntu Linux
Reading Email with PHP and IMAP/POP3
Garmin Edge 205 GPS Cycle Training Tool
Nasa's Space Shuttle Atlantis Transiting the Sun
Javascript Snippets
 
 

Reformat PHP code with RegEx in Eclipse, Dreamweaver etc

Eclipse and Dreamweaver both support regular expressions in search and replace. This is an immensely powerful and useful feature that makes otherwise tedious replacement tasks a breeze. In this article, a few expressions for some useful search / replace tasks are illustrated. Dreamweaver's regular expression search and replace is particularly handy for developers because you can do multi-line search and replace, and you can search and replace over an entire site or selected files in the site at once.
compton, 9 November 06
Updated 24 June 10
To use regular expressions in Eclipse or Dreamweaver searches, simply tick the Use regular expressions box on the search dialog. Useful RegEx features for Dreamweaver and Eclipse include the use of brackets to create a 'group'. The text which matches the RegEx within a pair of brackets can then be used in the replace string using $1 for the first bracketed group, $2 for the second bracketed group, and so on.

Escape HTML Form Values with Entites

When you wish to output PHP variables inside HTML elements, such as attribute values in an HTML form, some characters could royally fuck with your site, principally the quote character that delimits the attribute, or a less-than sign if you are so old-school (or just plain old) that you don't use any quote character around HTML attributes. Escaping these characters by passing the PHP variable through htmlspecialchars() is the way to go, but it's easily forgotten, and a pain to do afterwards. Thanks to the following RegEx, you can add this function later with no sweat:

Find:

"<\?= (\$.*?);? \?>"

Replace:

"<?= htmlspecialchars($1); ?>"

If for some reason you are averse to PHP short tags, use the following.

Find:

"<\?php echo (\$.*?);? \?>"

Replace:

"<?php echo htmlspecialchars($1); ?>"

Replace Deprecated ereg() Calls with preg_match() Equivalent

The POSIX compliant ereg() function is being deprecated from PHP in the near future. It's also not as efficient or as powerful as its PERL compatible brother, preg_match(). The following search/replace will also replace the case-insensitve eregi() with preg_match() and a RegEx case-insensitive modifier. Note that by design, the search pattern will not match any ereg() calls that contain a forward slash in the pattern, as they would not be correctly replaced. To fix any such instances, you can repeat the search but using a different pattern delimiter such as the hash character.

Find:

ereg(i?)\(['"]([^/'"]*?)['"], ?

Replace:

preg_match('/$2/'$1,

The search text will also not match if the RegEx contains a double or single quote. If you have many of those that you wish to replace, and you know all your strings use single quotes, you can easily rewrite the above:

Find:

ereg(i?)\('([^/]*?)', ?

Replace:

preg_match('/$2/'$1,

Replace ereg with Simple Logic Expressions

RegEx addicts have been known to use regular expressions to perform simple equality matches, for instance:

ereg( "^mars|jupiter$", $my_favourite_planet )

This can't easily be replaced with the equivalent logical expressions in a single step. The following will make it quicker though, by replacing those sorts of ereg calls with a single logical equality comparison that can then be changed as required.

Search:

ereg\("\^\(?([^\)]+)\)?\$",(\$[^\)]+)\)

Replace:

$2 == '$1'

Change from opening braces at end of line to hanging braces

Find:

( *)(.*)\s*\{

Replace:

$1$2
$1{


To enter a return character in the search/replace boxes, use Ctrl-Enter (Shift-Enter also works). Similarly, to fix any else statements, use the following after using the above:

Find:

( *)\}\s*else

Replace:

$1}
$1else


Both these assume that you use spaces to indent code. If tabs are used, then use (\t*) in place of ( *) in the search strings above.

Change spaces to tabs

If your code has spaces, and you wish to use tabs, it's easy enough to change those with search and replace. Regular expressions aren't needed. If your code uses three spaces for each level of indentation, just use 3 spaces as the search string, and a single tab character for the replace text. To do a tab character in Dreamweaver's search or replace fields, use Ctrl-Tab.

Change from double-quotes to single-quotes

Double quotes and single quotes are subtly different in PHP.
1 2 3