|
|
|
|
|
|
|
Reformat PHP code with Eclipse, Dreamweaver
|
|
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 30 June 09
|
To use regular expressions in Dreamweaver searches, simply tick the Use regular expressions box on the search dialog. Useful RegEx features for Dreamweaver 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.
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. A string enclosed in double quotes will be scanned and all tokens it contains will be replaced, while no such extra processing is performed on strings enclosed in single quotes. So if you have a string which does not contain any tokens (eg PHP variables), it's preferable to use single quotes.
In my job, I recently had to work with some very poorly written code, the least problem of which was use of double quotes when single ones would be (slightly) better. Only in the most extreme cases would this make any significant difference to a website's performance, but converting some of these is a good example for how RegEx can be used in Dreamweaver, or even Notepad++ should you prefer that like I do. The following RegEx would replace array key strings (eg $myarray["key"] - these indexes almost never contain tokens):
Find:
\["([^"]*)"\]
Replace:
['$1']
Instead of using the character class, [^"]*, to match the content of the string, you can make use of the \w RegEx escape character. It indicates a 'word' character ie any character which can be used in a word: \["(\w*)"\]
The RegEx can be quite easily expanded to also match strings inside normal brackets, for instance in the line include("myfile.php");, using the pipe "|" operator, which will match either the character to its left, or the one to its right. This defines a character class, so must be contained within square brackets. As we'll need to know whether a square or standard bracket was matched for the replace, the bracket-matching patterns will each have to be placed within their own set of normal brackets, making for a virtually unreadable RegEx:
Find:
([\[|\(])"(\w*)"([\]|\)])
Replace:
$1'$2'$3
To do the same in Notepad++ requires slightly different notation. Notepad++ RegEx doesn't support the \w escape character, so you need to specify a character class that does the same thing. Secondly, Notepad++ uses \1 etc instead of $1 in the replace string to indicate a group:
Find:
([\[|\(])"([^"]*)"([\]|\)])
Replace:
\1'\2'\3
Replace Strings set to empty string just before being assigned a valueFind:
\$sql = "";\s*\$sql \.= "
Replace:
\$sql = "
Add whitespace around logical equality operatorsOf all the keys on the keyboard to avoid pressing, the spacebar is the least worthy. |
|
1 2 |
|
|
|
|
|
|