2015-05-14 121 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標籤保存它。 –