PHP syntax highlighting
Since no existing tool satisfied my needs, I wrote my own. Lo and behold:
Hyperlight
Usage is extremely easy: just use
<?php hyperlight($code, 'php'); ?>
to highlight code. Writing new language definitions is relatively easy, too – using regular expressions and a powerful but simple state machine. By the way, I still need a lot of definitions so feel free to contribute.
Syntax highlighter in html/php page
There's a difference between a "syntax highlighter" that will correctly format a chunk of code to display it optimally, and a syntax highlighting code editor, that allows a user to enter code that will be colored according to the selected language's syntax.
For the latter I have been using CodeMirror which works very well. But there are many other solutions, the relevant WikiPedia page contains an extensive list and a feature comparison.
EDIT: to get you started: start by reading the manual, it shows the different possible scenarios for integrating CodeMirror in your code. I think that in your case just using a simple CodeMirror.fromTextArea()
should get you started. CodeMirror comes with tons of examples, like this one highlighting php. Have a look at the source code, I pasted the relevant part here below:
<form><textarea id="code" name="code">
<?php
function hello($who) {
return "Hello " . $who;
}
?>
<p>The program says <?= hello("World") ?>.</p>
<script>
alert("And here is some JS code"); // also colored
</script>
</textarea></form>
<script>
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
lineNumbers: true,
matchBrackets: true,
mode: "application/x-httpd-php",
indentUnit: 4,
indentWithTabs: true,
enterMode: "keep",
tabMode: "shift"
});
</script>
It's a simple as pointing CodeMirror.fromTextArea
to the text area you want to highlight and (optionally) setting options to finetune its behaviour.
Sublime 3 editor syntax highlighting PHP + HTML not working
There is a thread on Sublime forum about this, it should be fixed in next development version.
You could maybe downgrade to a working version until its fixed properly.
https://forum.sublimetext.com/t/php-detection-broken-in-3103/17355
How do you display your php source code with highlight or view source?
PHP has two native functions that might be of interest: highlight_file()
and highlight_string()
. If neither of those are ideal, you could also use Google Code Prettify to achieve this result. This is the solution many use, including StackOverflow itself.
Alternatives:
- SyntaxHighlighter
- SHJS
- jQuery Chili
- Lighter for MooTools
- GeSHi
How do I use syntax highlighting in PHP within a markdown github gist?
Fenced code blocks do work in Markdown Gists, and in fact your code is being rendered that way. If you inspect the blocks you'll see that they are contained in div
s with class="highlight highlight-PHP"
.
The problem is that PHP code is only recognized for highlighting by GFM if it includes the <?php
delimiter (much like PHP code only runs inside a <?php
block). Add this to the top of each PHP code block and you should be good to go, e.g.:
...
```php
<?php
class GO_Example_Model_Thing extends GO_Base_Db_ActiveRecord {
...
PHP Syntax Highlight in Sublime Text
You just need to have a color scheme that has the correct scopes. For example, this is how similar code looks like using the Neon Color Scheme:
Related Topics
What Does It Mean to Run PHP in Quiet Mode
Fatal Error: Call to Undefined Method MySQLi::Error()
Get Content Within a HTML Tag Using PHP and Replace It After Processing
Forcing Fputcsv to Use Enclosure for *All* Fields
How to Rename a Tag in Simplexml Through a Dom Object
How Random Is PHP's Shuffle Function
How to Remove <Br /> Tags and More from a String
Extract Floating Point Numbers from a Delimited String in PHP
Data Transfer from JavaScript to PHP
My Laravel 5.2.10 Sessions Wont Persist
How to Do Url Rewriting in PHP
How to Have a Stable Sort in PHP with Arsort()
PHP Sending Variables to File_Get_Contents()
Laravel SQLstate[Hy000] [2002] Connection Refused