Yahya Uddin
Yahya Uddin

Reputation: 28841

PHP Writing long strings on separate lines

I wanted to write a line of code that contains a long string such as:

addError("This is a really really really really really really really long text");

however I wanted to split the text into multiple lines. How can this be possible aside from this way:

addError("This is a really really really really really really" .
       "really long text");

EDIT: I need it such that it dosen't do line breaks either. (forget about that SQL thing i said earlier)

Upvotes: 0

Views: 3506

Answers (3)

Denis de Bernardy
Denis de Bernardy

Reputation: 78433

If SQL syntax highlighting is your problem, you can probably format it properly using a heredoc, and still get the IDE's syntax highlighting working:

$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

If you don't want line breaks, your way of doing it now seems right. With the above, you'd need to remove the line breaks afterwards, e.g. str_replace("\n", " ", $str).

Upvotes: 6

dev7
dev7

Reputation: 6369

Assuming you are trying to break the output to rows, you can use str_split( $string, $chunk_length ).

This will split the long text into array of strings with a fixed length.

$array_of_strings = str_split($str, 100);//100 represents the length of the chunk
foreach ($array_of_strings as $line) {
  echo $line . '<br>';
}

You can also use chunck_split();

Hope this helps!

Upvotes: 0

RiggsFolly
RiggsFolly

Reputation: 94642

Try this :-

$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 );

Upvotes: 1

Related Questions