Php. Displaying long string as Java code display
I have a problem that I need help with. with php.
Let's say I have a long string.
s = "public class Main{public static void main(String args[]){System.out.print("Hello");}}";
(Ignore the fact that "hello" is in quotes.
How can I display this string in a webpage either with php or html so it looks like
public class Main{
public static void main(String args[]){
System.out.print("hello");
}
}
I am getting this line from mysql query so there are no new lines. A string stored in the database via the form (although it was written with correct lines and spaces) that was passed to the php script as a long string (striptags were used).
Is there a quick way to format this long string as coding style, or should I do this from the moment it is passed to a php script that stores it in the database.
Any help and suggestions would be appreciated. Thank you very much.
source to share
How do I use the JavaScript library to format the output? Here is an example using google-code-prettify
eg. 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 drawback of the string you provided is that it doesn't contain any new characters in the string, so the result looks flat. Can you store a Java string in your DB with newlines so that it looks better when exited?
eg. 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>
source to share