我想编写一行包含长字符串的代码,例如:
addError("This is a really really really really really really really long text");
但是我想将文本分成多行。如何通过这种方式抛开:
addError("This is a really really really really really really" .
"really long text");
编辑:我需要这样它也不会做换行符。 (忘记我之前说的SQL事件)
答案 0 :(得分:6)
如果SQL语法突出显示是您的问题,您可以使用heredoc正确格式化它,并且仍然可以使IDE的语法突出显示正常工作:
$query = <<<EOD
select *
from foo
where bar
EOD;
do_query($query);
http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
如果你不想换行,你现在的做法似乎是正确的。有了上述内容,您需要在之后删除换行符,例如str_replace("\n", " ", $str)
。
答案 1 :(得分:1)
试试这个: -
$t = 'This is a really really really really really really';
$t .= ' really really really really really really';
$t .= ' really really really really really really';
$t .= ' long string';
addError( $t );
答案 2 :(得分:0)
假设您尝试将输出分解为行,可以使用str_split( $string, $chunk_length )。
这会将长文本拆分为具有固定长度的字符串数组。
$array_of_strings = str_split($str, 100);//100 represents the length of the chunk
foreach ($array_of_strings as $line) {
echo $line . '<br>';
}
您也可以使用chunck_split();
希望这有帮助!