Display code (HTML, CSS, PHP, JavaScript, jQuery, etc.) on a webpage, just like code is displayed here, in a box, syntax-highlighted

后端 未结 5 1722
不知归路
不知归路 2021-02-20 06:07

I\'d like to display code on a web page. However I\'d like it to keep its spacing and be color coded by syntax.

Please do not respond with:

5条回答
  •  感情败类
    2021-02-20 06:32

    This is what you need to do:

    1. Replace < > & " with entities such as < You do this, then do #2 to preserve spacing. You cannot avoid escaping entities since those characters can cause invalid HTML which causes parsing errors in the browser.
    2. Use
       to preserve spacing. After you escaped those special characters,  and  will not cause the escaped HTML and PHP to get executed (unless you explicitly told PHP and HTML to execute them).

    Syntax coloring is a bit more difficult, since you will need a parser that understands the language you're trying to show; even Stack Overflow's syntax coloring does not always work as well as it could.

提交回复
热议问题