Highlighting partial documents

I need to show a section of 9 lines from within a full file that may be a mix of PHP, JS, HTML or CSS. The 9 lines may not/like won’t include required start or end tags at the start/end of the section. eg:

$xyz = 3;
$abc = 4;
echo $xyz*$abc;
<p>This is a demo of something.<p>
<p>Here's some JS</p>
var a = 7;
var b = 8;

What would be the best way of getting this shown with the good syntax highlighting you would typically see?

Looking at my above example, it seems CodeMirror will highlight unclosed chunks of code (eg <script> onwards) but just doesn’t handle the PHP at the beginning.

If you know that a buffer will start in already-open PHP code, you can use the startOpen option to the mode (i.e. mode: {name: "php", startOpen: true}). The mode does not determine this automatically, since that would involve arbitrary and complex lookahead.

Brilliant, thanks - didn’t know that param was available to use.