0

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)

8
  • 1
    If you want a line feed in the actual error text, use "\n" or PHP_EOL Commented Jan 28, 2014 at 20:31
  • 1
    There are a few ways. A Heredoc would possibly be your best bet, assuming your IDE doesn't get confused by them in the same way. Commented Jan 28, 2014 at 20:33
  • 1
    Then I'd suggest upgrading your IDE. While it's nice that it can detect "other languages" inside a PHP string and format them, if it can't handle perfectly valid php syntax, then that's the IDE's fault, not something you should have to work around. Commented Jan 28, 2014 at 20:39
  • Just out of interest, what IDE are you using? Commented Jan 28, 2014 at 20:41
  • @Mark Baker no I DONT want line breaks. Commented Jan 28, 2014 at 20:41

3 Answers 3

6

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).

Sign up to request clarification or add additional context in comments.

6 Comments

This will produce line breaks. OP don't want line breaks (he told)
Oh, I see, OP edited the question. Then it's a +1 :)
@hek2mgl: I'm not seeing that in the question, yeah. I only see the reference to SQL syntax highlighting gone wrong when he breaks the query in multiple strings. Plus, OP really should format his SQL queries if possible, as it makes things a lot easier to read. :-)
Yeah I need it such that it dosen't have line breaks
Isn't it perfectly valid for SQL statements to have line breaks?
|
1

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

1 Comment

Yeah that produces the same problem. I don't want to use any concatenation.
0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.