2015-05-14 97 views
3

我有一个问题,我需要一些帮助。与PHP。在php中。显示一个长字符串作为java代码显示

说我有一个很长的字符串。

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

(忽略这一事实,“你好”是加引号。

我怎样才能在网页上显示该字符串,要么是使用PHP或HTML,使它看起来像这样

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

    System.out.print("hello"); 

    } 

} 

我从一个mysql查询中获取这个字符串,因此没有新行。通过表单存储在数据库中的字符串(尽管它是用正确的行和空格编写的),它以长字符串形式传递给一个php脚本(striptags被使用)。

有没有一种快速的方法来将这个长字符串格式化为编码风格,还是应该从传递给将其存储在数据库中的php脚本开始。

任何帮助和建议将bvery赞赏。非常感谢你。

回答

0

如何使用JavaScript库格式化输出?以下是使用google-code-prettify

例如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> 

为您提供的字符串唯一的缺陷是,它不包含任何新行字符,所以输出相貌平平。 您是否可以用新行将Java字符串保存到数据库中,以便在输出时看起来更好?

例如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> 
+0

Teah。我需要开源。我也可以调用这个php并传递s。无论如何,我被允许将这个数据库存入数据库。只要它不会破坏我的计划。例如,我可以(如果它有帮助,或可能)用html标签保存它。 –