2015-04-04 56 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