【问题标题】:PhP: Breaking an array into more linesPhP:将数组分成更多行
【发布时间】:2015-04-04 14:55:54
【问题描述】:

多年来,我一直在编程 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;

【问题讨论】:

  • 如何将数组分成更多行?您的意思是在代码中分成多行或多个元素??
  • “在代码中”,但我也对“多个元素”感兴趣。
  • 刚刚再次查看了您的代码,两次尝试都应该可以正常工作!你有任何错误吗?添加报错:<?php ini_set("display_errors", 1); error_reporting(E_ALL); ?>
  • 这两个示例在命令行上对我来说都执行得很好。对“没有成功”有一个更清晰的定义会有所帮助。
  • 所以你可以像第一次尝试那样连接你的字符串,问题出在哪里?

标签: php arrays


【解决方案1】:

静态类变量不能用 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"
}

【讨论】:

  • OP 连接字符串的问题在哪里? 静态类变量不能用 PHP 中的表达式初始化你有这个(官方)手册链接吗?
  • 请参阅this stackoverflow 问题以获取手册中的引用。
  • 所有其他解决方案都不起作用(至少对我来说) - 我也尝试过不使用 yii-framework。但是 user3760780 的两个替代方案工作正常,谢谢!
  • Rizier123 我也对您的解决方案感兴趣。您可以将整个代码复制到答案中吗?我会在这里测试一下。
  • @Didgejo 您在这里向我们展示的代码完全可以 100% 正常工作!或者等等... PHP echo phpversion(); 吗?
猜你喜欢
  • 2011-02-21
  • 1970-01-01
  • 2014-07-19
  • 1970-01-01
  • 1970-01-01
  • 2012-01-31
  • 1970-01-01
  • 2012-07-07
  • 1970-01-01
相关资源
最近更新 更多