How do I easily highlight the syntax of PHP code in Word?
Solution 1
You can use Notepad++ to accomplish this in three ways. Just so you know, Notepad++ is a more advanced version of Notepad, which supports syntax highlighting of different code files "out of the box" - PHP included!
Download & install it, fire it up, and load up your PHP file. You should automatically see it beautifully coloured (if not, because the file extension is something other than .php
, go to Language -> PHP or Language -> P -> PHP).
If you need to change any of the colours, you can easily do so - just go to Settings -> Styler Configurator. From that menu, you can change the various highlighting and font options, to suit your needs - although the default usually suffices for most.
Then, go to Plugins -> NppExport. From there, you have three options you can consider:
- Export to RTF
- Export to HTML
- Copy all formats to clipboard
Start with the last one - "Copy all formats to clipboard" - which will copy the entire file with the highlighted syntax to the clipboard. Once you click it, then open Microsoft Word, and just hit paste! You should see the beautifully syntax-highlighted code. If something goes wrong, then you can try one of the other options (export to RTF/HTML), although I've never had a problem with the clipboard method.
Solution 2
Send your code to an online syntax highlighting website, this one for example (select the langage yourself, Autodetect doesn't work very well). There is a list of other online syntax highlighting site here.
Copy the highlighted code and paste it into Word.
Solution 3
You could install the .PHPS file type into Apache then copy the output from your browser into Word.
in your http.conf
AddType application/x-httpd-php-source .phps
Solution 4
I found this question looking for the same thing for Outlook on Mac OS, but Notepad++ doesn't exist for OSX and I use Sublime Text (which has the advantage of being cross-platform).
For this there's a plugin called SublimeHighlight.
Assuming you use Sublime Package Control, the steps to install SublimeHighlight are:
- Press cmd shift P
- Run "Package Control: Add channel"
- Paste the github url: https://github.com/n1k0/SublimeHighlight
- Press cmd shift P again, and run "Package Control: Install Package"
- Install the "Highlight" Package, and restart Sublime Text.
Then to copy text you can press cmd shift P, and run "SublimeHighlight: copy to clipboard as HTML" (or RTF). These commands can also be assigned keyboard shortcuts.
Note that on Windows or Linux the shortcut key for the Command Palette is different, but it can be accessed by going to Tools > Command Palette.
Solution 5
Syntax highlighting many languages in Word documents: http://www.planetb.ca/2008/11/syntax-highlight-code-in-word-documents/
Related videos on Youtube
Christian Studer
The sky above the port was the color of television without an input signal. The message Check cable kept flashing, despite my most desperate efforts to configure the display settings. The audience was getting restless. "Who is that guy?"
Updated on September 17, 2022Comments
-
Christian Studer over 1 year
How do I easily write and syntax-highlighted PHP code (or any others programming language) in a Word 2009 document?
-
MT. over 14 yearswhy would you want to?
-
D'Arvit over 14 yearsI would just use Notepad++. It's great and has syntax highlighting for hundreds of languages
-
Christian Studer over 14 years@RCIX: I'm writing a concept document which will feature only code snippets, but no actual code.
-
-
Vilx- over 14 yearsNote: you have to use IE for this. Opera doesn't work, but I don't know about FF/Safari/Chrome.
-
Breakthrough over 14 yearsSome browsers carry over formatting and colour when you copy & paste. ;)
-
Snark over 14 yearsWorks fine with Chrome and Firefox.
-
Christian Studer over 14 yearsThanks, I already had Notepad++ installed, but didn't knew about the NppExport-plugin. I'll go with your solution.
-
Christian Studer over 14 yearsCreative, but not exactly easy (Requires a webserver...). ;-)
-
Breakthrough over 14 yearsYeah, it's quite useful for copying & pasting to Word to print documents out... I love it. :)
-
Josh Hunt over 14 yearsThis is why I love SU. I had no idea you could do this!
-
Shrike over 14 yearsFair point :)
-
dcousineau over 14 yearsAbsolute perfection!
-
Ray about 9 yearsNice. One additional tip (half related): You can then highlight the pasted text, then on the Review tab of word, select Language, Set Proofing Language. Tick 'Do not check spelling or grammar'. Now you won't get the red underlines JUST for the pasted text.
-
David about 9 yearsI couldn't get this working from Firefox to Word 2013 for some reason. IE worked fine.
-
Ismael Miguel almost 9 yearsNotice that if you have the option to create links enabled (Notepad++ now created a clickable link when something starts with http:// or similar), NppExport will throw an
Unknown Exception
. -
CharlieRB about 8 yearsWelcome to Super User. Please realize you have posted an answer to a question asked more than 5 years ago, and which already has an accepted answer. Therefore, you may not receive acknowledgement.
-
Eliahu Aaron almost 5 yearsIn Notepad++ v7.7.1, NppExport maintains font name, size, and bold, but not font color. Works fine in 7.6.x. See: NppExport plugin - 'copy HTML to clipboard' does not work anymore
-
Nicicalu about 2 yearssyntax-highlighter.k26.ch This one works great