Sublime Forum

Validate PHP Syntax

#1

I’m trying to replicate TextMates ctrl+shift+v Validate Syntax, specifically for PHP. It’s handy to quickly check if there are any errors before saving a remotely-edited file, instead of finding the error in the web browser.

This is the command used in TM:

#!/usr/bin/env ruby require ENV'TM_SUPPORT_PATH'] + '/lib/textmate' version = %x{#{ENV'TM_PHP'] || 'php'} -v}.split[0..2].join(' ') puts "Running syntax check with " + version + "…" result = `#{ENV'TM_PHP'] || 'php'} -d display_errors=on -l` puts result.gsub('in -', '') TextMate.go_to :line => $1 if result =~ /line (\d+)/

Any suggestions as to the best way to reproduce this in ST2? My first attempt was along the lines of:

{ "keys": "ctrl+shift+v"], "command": "exec", "args": { "cmd": "php -d display_errors=on -l $TM_FILENAME"]} }

… but that obviously didn’t work. Sorry for the dumb question, I know it should be dead easy, but I’m new to learning ST2 and any help is appreciated.

0 Likes

#2

Any input on this? I miss this too :frowning:

0 Likes

#3

I setup a simple build file for this. Here’s what I have:

{ "cmd": "c:\\xampp\\php\\php.exe", "-l", "$file"], "selector": "source.php" }

Just change the c:\xampp\php\php.exe to the path of your php executable

It would be nicer as a Plugin to tie in when saving but I haven’t gotten far enough in Python to figure that out.

0 Likes

#4

Oh, and to use a build file, save that as PHP.sublime-build in your PHP package (Preferences > Browse Packages). Then hit F7 or Tools > Build to run it.

0 Likes

#5

Can’t you just turn on build on save for PHP files? I knew you could in Sublime 1.

0 Likes

#6

I haven’t seen any options for that but it might be handy. Probably needs to be a per-language setting, right?

0 Likes

#7

Hey thanks Nooge, it worked flawlessly! Would be cool that it show the error in a tooltip, but oh well! and I didn’t find how to build on save, any tips to do that for Sublime text 2?

0 Likes

#8

What happened to lineNumberRegex? Or for ST2, line_number/line_number_regex

Wham. Even better.

{ "cmd": "c:\\xampp\\php\\php.exe", "-l", "$file"], "selector": "source.php", "file_regex": "^Parse error: .* in (.*?) on line ([0-9]*)" }
Press F4 to jump to the line :smile:

0 Likes

#9

Hey that’s nice! Just missing build on save and I’m an happy guy!

Also, is there any way to make the console that pops a fixed height each time I open a window or I start Sublime? I have to resize it everytime :smile:

0 Likes

#10

Unless Jon has added an option for it, I don’t think there is.

0 Likes

#11

Would you happen to know how I would do this for a Mac environment? I am trying to setup Sublime to validate PHP using my Mac and am running into problems. I am using MAMP (also, I know Mac’s come with PHP built in so I could use that as well).

Thanks!

0 Likes

#12

[quote=“macphreak”]Would you happen to know how I would do this for a Mac environment? I am trying to setup Sublime to validate PHP using my Mac and am running into problems. I am using MAMP (also, I know Mac’s come with PHP built in so I could use that as well).

Thanks![/quote]

Sure!

"]

Tools > Build System > New Build System…
Paste in:

{ "cmd": "/usr/bin/php", "-l", "$file"] , "selector": "source.php" , "file_regex": "^Parse error: .* in (.*?) on line ([0-9]*)" }
Now, just hit ⌘-b to validate any PHP file.

0 Likes