PHP long string without newline
Asked Answered
M

5

6

I have a long string in php which does not contain new lines ('\n').

My coding convention does not allow lines longer than 100 characters.

Is there a way to split my long string into multiple lines without using the . operator which is less efficient - I don't need to concat 2 strings as they can be given as a single string.

Thanks!

Y

Mushy answered 18/8, 2009 at 12:2 Comment(0)
M
3

That's easy:

$str  = 'long text long text long text long text long text';
$str .= 'long text';
Mishmash answered 17/8, 2023 at 13:40 Comment(0)
D
1

You could strip newline characters from the string:

  $myStr = "
    here is my string
    and it is spread across multiple lines
  ";
  $myStr = str_replace("\n", "", $myStr);  

You should still use the concatenation operator (.) for such things. The performance penalty here is negligible if you're using opcode caching (APC) and this would really not be anything noticeable when taking DB access and additional logic (including loops) into the run time equation.

Update:

Give the page below a read. The conclusion above is not readily available in the text, but a careful examination should show it is valid.

http://blog.golemon.com/2006/06/how-long-is-piece-of-string.html

Duplication answered 18/8, 2009 at 12:7 Comment(3)
How does opcode caching affect the speed of string concatenation?Thermopile
The parser would treat strings with no dynamic elements to be one long string, dropping the concatenation operators. I've dug into this subject a while ago. Will update this answer with the related article once I manage to find it again.Duplication
It get's me " here is my string and it is spread across multiple lines ". Doesn't look like any sensible resultGravestone
R
0

This is the right code, that solved all issues:

$myStr = "
    here is my string
    and it is spread across multiple lines
  ";
  $myStr = str_replace(array("\r","\n"), "", $myStr);  

It is based on Lior Cohen's answer, but it also strips carriage returns "\r".

Renovate answered 28/11, 2016 at 1:37 Comment(1)
It get's me " here is my string and it is spread across multiple lines ". Doesn't look like any sensible resultGravestone
C
0

why not:

 $myStr = "here is my string "
   . "and it is spread across "
   . "multiple lines";

you are just appending over multi lines

Charmain answered 15/7 at 15:39 Comment(0)
T
-1

With the heredoc syntax (or nowdoc, depending on your needs, check the documentation link):

$multiline = <<<EOT
My name is "$name". I am printing some $foo->foo.
This should print a capital 'A': \x41
EOT;
$singleline = str_replace("\n","",$multiline);

But this sucks... sorry :-)

Telegonus answered 18/8, 2009 at 12:6 Comment(3)
The problem with heredoc is that it will include the newline characters in the string, which would then have to be stripped out to get the "string which does not contain newlines" that the question specified.Squat
Yes, however that would also have a performance hit just as using concatenation would.Squat
But I think the hit would be slower... One would have to measure.Telegonus

© 2022 - 2024 — McMap. All rights reserved.