2

I need to truncate a text before encoding it in base64. I try with wordwrap :

$body_wrap = wordwrap($arrayInfo['body'], 150, '\n', false);

Using '\n', '\r', '
', this is not working.

Is there another solution to wrap my text ? Because in HTML, I have big lines that overflow the screen

Nathan30
  • 689
  • 2
  • 8
  • 29
  • 2
    If you want `\n` to really be a newline character you have to use double quotes, not single quotes, like so: `"\n"`. If it is HTML then `'
    '` would be a newline, or you have to surround the text by the preformatted `
    ...
    ` tag.
    – KIKO Software Oct 06 '17 at 08:42
  • The "big lines that overflow the screen" issue should be solved on the client side. Screens differ, and PHP knows nothing about its size. The solution you are looking for lays in CSS ground. – Alex Blex Oct 06 '17 at 08:45
  • Thanks Kiko, it's perfect. @AlexBlex I can't modify easily the client side, so the best way to do what I need to, is the wordwrap – Nathan30 Oct 06 '17 at 08:48

1 Answers1

2

Try the following:

$body_wrap = wordwrap($arrayInfo['body'], 150, "\r\n", false);

or

$body_wrap = wordwrap($arrayInfo['body'], 150, PHP_EOL, false);

1st one, uses " double quotes to wrap the \r\n

Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.

2nd one, uses PHP_EOL constant which is OS independent.

mega6382
  • 9,211
  • 17
  • 48
  • 69
  • Thanks a lot. Just a question : What the difference between "\n" and "\r\n" ? – Nathan30 Oct 06 '17 at 09:00
  • @Nathan30 Check Here: https://stackoverflow.com/questions/15433188/r-n-r-n-what-is-the-difference-between-them – mega6382 Oct 06 '17 at 09:00
  • @Nathan30 It is about OS, while `\r` will add a new line on OS X it will not on Windows etc. That is why they are used together. To support multiple platforms. – mega6382 Oct 06 '17 at 09:02