3

I'm trying to add new line at the end of string, so I could better see <option> in new lines, hovewer I get string with "\r\n" as text instead of new lines. What is wrong with code ?

foreach ($xml['ROW'] as $ar) {
   $tekstas = $ar['zz'] . ' (' . $ar['xx'] . ')';
   $insert .= '<option value="' . $ar['Adresas'] . '">' . $tekstas .'</option> "\r\n"' ;
}
echo nl2br(htmlentities($insert));
JJonas
  • 49
  • 1
  • 2
  • 10

3 Answers3

3

Almost there, look at the right way to concatenate the new line to the rest of the string you generate.

foreach ($xml['ROW'] as $ar) {
 $tekstas = $ar['zz'] . ' (' . $ar['xx'] . ')';
 $insert .= '<option value="' . $ar['Adresas'] . '">' . $tekstas   .'</option> ' . PHP_EOL ;
}
echo nl2br(htmlentities($insert));
Twisted1919
  • 2,430
  • 1
  • 19
  • 30
  • 1
    `\r\n` is used on Windows and `\n` on UNIX based systems. if you use the constant `PHP_EOL`, it will be more compatible on both. – HPierce Jul 30 '15 at 16:09
2

'-quoted strings do not honor escaped metacharacters like "-quoted ones do:

echo '\r' - outputs a literal backslash and an 'r'
echo "\r" - outputs a carriage return

the only escapes that are supported in ' strings are \' and \\.

So..

'</option> "\r\n"' 
^---open single quote string
                 ^---close single-quote string

when it should be

'</option> ' . "\r\n"

instead.

Marc B
  • 356,200
  • 43
  • 426
  • 500
1

"\r\n" should be in double quotes.

foreach ($xml['ROW'] as $ar) {
   $tekstas = $ar['zz'] . ' (' . $ar['xx'] . ')';
   $insert .= '<option value="' . $ar['Adresas'] . '">' . $tekstas .'</option>'."\r\n";
}
echo nl2br(htmlentities($insert));
Søren Beck Jensen
  • 1,676
  • 1
  • 12
  • 22