|
|
|
|
|
|
|
Reformat PHP code with RegEx in Eclipse, Dreamweaver etc
|
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
Fix unclosed HTML tags for XHTML ComplianceOlder HTML standards allowed single tags such as img and input which in order to be XML compliant for XHTML need to be closed. The following RegEx can be used to search and replace these.
Find:
<(input|img) ([^>]*)([^/])>
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. After all, it has nothing printed on it so you need not worry about wearing away the letters.
To add spaces before and after operators in if statements, this will help:
([^ !])([=<>!]==?)([^ !])
Replace:
$1 $2 $3
Restore Missing SemicolonsHere's another trivial and inconsequential annoyance: missing semicolons/spaces, in particular at the ends of the last PHP statement of a scriptlet. The semicolon can acceptably be dropped if you use the shorthand <?= $var ?>, but if you prefer to use the equivalent <?php echo $var; ?>, a semicolon should strictly be present. Spaces always help readability and there's never an excuse for stingy spacebar usage, other than being a noob.
;? ?\?>
; ?>
There's no good reason not to use the shorthand syntax - under the hood they are identical - but consistency is important. Similarly, to replace the shorthand |
|
1 2 3 |
|
|
|
|
|
|