在PHP中。 将长字符串显示为java代码显示

I have a problem that I need some help with. with php.

Say I have a long string.

s = "public class Main{public static void main(String args[]){System.out.print("Hello");}}";

(Ignore that fact that "hello" is in quotes.

How can I display this string in a webpage either using php or html so that it looks like this

public class Main{
    public static void main(String args[]){

    System.out.print("hello");

    }

}

I am getting this string from a mysql query, therefore no new lines. The string stored in the database through a form(although it was written with the correct lines and spaces), that was passed to a php script as a long string(striptags was used).

Is there a quick way to format this long string as a coding style or should i do it from the moment that it was passed to the php script that stores it in the database.

Any help and suggestions will be bvery appreciated. Thank you very much.

How about using a JavaScript library to format the output? Here's an example using google-code-prettify

e.g. https://jsfiddle.net/rLw63jro/

<script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js"></script>

<pre class="prettyprint linenums">
public class Main{public static void main(String args[]){System.out.print("Hello");}};
</pre>

The only flaw for the string you provided is that it doesn't contain any new line characters, so the output looks flat. Are you able to save the Java string into your DB with new lines so when it get output it will look nicer?

e.g. https://jsfiddle.net/opz9e30y/

<script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js"></script>

<pre class="prettyprint">
public class Main{
    public static void main(String args[]){

    System.out.print("hello");
    }
}
</pre>