PhP:将阵列分成更多行

时间:2015-04-04 14:55:55

标签: php arrays

我从很多年开始编程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;

1 个答案:

答案 0 :(得分:0)

静态类变量可能无法使用PHP中的表达式进行初始化。

这是通过HEREDOC-Syntax拆分字符串的一种方法,只要它可能包含额外的空格和换行符:

<?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"
}