2015-04-04 78 views
-1

我從很多年以來一直在編程php。現在我有了一個新的願望。我想和一個長字符串更可讀的數組:PhP:將數組分解成多行

$var = array('help' => 'This is a very long help text. This is a very long help text. This is a very long help text.'); 

首先嚐試:我已經試過這沒有成功:

$var = array('help' => 'This is a very long help text.' 
. 'This is a very long help text.' 
. 'This is a very long help text.'); 

第二個嘗試:我已經試過這沒有成功:

$var = array('help' => 'This is a very long help text.' . 'This is a very long help text. This is a very long help text.'); 

順便說一句:有沒有機會建立字符串之前,我必須在這一行。如何將數組分成更多行?

我被要求展示真實的例子,那就是:

class JKWerte { 
    public static $purifyoptions = array(
    'HTML.Allowed' => 'table, thead, tbody, tfoot, th, tr, td, img[src|alt], div, span, p, br, ul, ol, li, *[class], *[style], *[height], *[width], h1, h2, h3, h4, a[href|title], b, i, em, strong'); 

    public function test() { 

    } 
} 

變量是一個類的屬性,我和 JKWerte :: purifyoptions調用它;

+0

*我怎樣才能打破陣列爲更多的行*您指的是在代碼分成多行或多個元素? – Rizier123 2015-04-04 14:57:20

+0

「在代碼中」但我也對多個元素感興趣「。 – Didgejo 2015-04-04 14:59:27

+0

只需再次查看您的代碼,兩次嘗試都應該正常工作!你有任何錯誤?添加錯誤報告:'<?php \t \t ini_set(「display_errors」,1); \t \t error_reporting(E_ALL); \t?>' – Rizier123 2015-04-04 15:06:07

回答

0

靜態類變量不能用PHP中的表達式進行初始化。

這裏是分裂通過HEREDOC語法字符串的一種方式,只要它可能含有額外的空間和換行:

<?php 
class JKWerte { 
    public static $purifyoptions = array(
    'HTML.Allowed' => <<<EOT 
    table, thead, tbody, 
    tfoot, th, tr, td, 
    img[src|alt], div, 
    span, p, br, ul, ol, 
    li, *[class], *[style], 
    *[height], *[width], h1, 
    h2, h3, h4, a[href|title], 
    b, i, em, strong 
EOT 
    ); 

    public function test() { } 
} 

var_dump(JKWerte::$purifyoptions); 
?> 

輸出:

array(1) { 
    ["HTML.Allowed"]=> 
    string(213) "  table, thead, tbody, 
    tfoot, th, tr, td, 
    img[src|alt], div, 
    span, p, br, ul, ol, 
    li, *[class], *[style], 
    *[height], *[width], h1, 
    h2, h3, h4, a[href|title], 
    b, i, em, strong" 
} 

另外,您可以定義內容以後你的靜態變量,例如類定義後:

<?php 
class JKWerte { 
    public static $purifyoptions = array(
    'HTML.Allowed' => '' 
    ); 

    public function test() { } 
} 

JKWerte::$purifyoptions['HTML.Allowed'] = 'table, thead, tbody, ' 
    .'tfoot, th, tr, td, ' 
    .'img[src|alt], div, ' 
    .'span, p, br, ul, ol, ' 
    .'li, *[class], *[style], ' 
    .'*[height], *[width], h1, ' 
    .'h2, h3, h4, a[href|title], ' 
    .'b, i, em, strong'; 

var_dump(JKWerte::$purifyoptions) 
?> 

輸出:

array(1) { 
    ["HTML.Allowed"]=> 
    string(172) "table, thead, tbody, tfoot, th, tr, td, img[src|alt], div, span, p, br, ul, ol, li, *[class], *[style], *[height], *[width], h1, h2, h3, h4, a[href|title], b, i, em, strong" 
} 
+0

OP連接字符串的問題在哪裏? *靜態類變量可能無法用PHP中的表達式進行初始化*您是否有(官方)手動鏈接? – Rizier123 2015-04-04 15:32:42

+0

請參閱[本](http://stackoverflow.com/questions/4267050/strange-parse-error-with-static-concatenated-string-variable)從手冊報價的stackoverflow問題。 – aleju 2015-04-04 15:45:27

+0

所有其他解決方案不起作用(至少對我而言) - 我也嘗試過沒有yii-framework。但是,來自用戶3760780的兩個選擇正常工作,thanx! – Didgejo 2015-04-04 15:47:05