0

I use Linux server for my CakePHP application. I use PHP 5.4.30.
I need to str_replace newlines from a string. I want to use PHP_EOL.

But var_dump(PHP_EOL) gives this:

string(1) " "

It is strange to get empty string for this constant. I was waiting for \n or \r.

This works:

$text=str_replace("\r"," ",$text);

This doesn't work:

$text=str_replace(PHP_EOL," ",$text);

(All my files are UTF8 encoded)

Solution:

str_replace(PHP_EOL..)

didn't work because I get text from a Windows server and process it in Linux server. So Linux's PHP_EOL didn't find the newlines in text. Problem solved when I str_replace both windows and unix newline characters.

Community
  • 1
  • 1
trante
  • 33,518
  • 47
  • 192
  • 272
  • So does that constant still work in your replace? Does it replace new lines..? That can have just something to do with encoding.. – Hardy Mar 09 '14 at 00:35
  • PHP and CakePHP versions? See this: http://cakephp.1045679.n5.nabble.com/PHP-EOL-bug-in-cake-1-2-td4258542.html – Hardy Mar 09 '14 at 00:46
  • PHP 5.4.30, CakePHP 2.4.4. – trante Mar 09 '14 at 00:48
  • 2
    var_dumping `\n` won't print `\n`, it will print a newline. Which, if you're using browser to view the script's output, will be rendered as space. – lafor Mar 09 '14 at 00:48
  • See this also.. it may help.. http://stackoverflow.com/questions/128560/when-do-i-use-the-php-constant-php-eol – Hardy Mar 09 '14 at 00:48
  • @lafor But also `str_replace(PHP_EOL,...)` doesn't work. – trante Mar 09 '14 at 00:50
  • 1
    Most likely because your files' EOLs are different from PHP_EOL. Do `var_dump(PHP_EOL === "\n")` – lafor Mar 09 '14 at 00:53
  • @lafor Thank you. I get the error after your comment. I get text from Windows API server and parse it in Linux server. – trante Mar 09 '14 at 11:15

2 Answers2

0

Use this:

$text = str_replace(array("\r\n", "\n\r", "\n", "\r"), ' ', $text)

This should cover all the cases regardless of where the system is hosted on.

You can rely on PHP_EOL if all the data is constructed in that OS that is manipulating it, otherwise it is best to use a specific newline character.

Aziz Saleh
  • 2,687
  • 1
  • 17
  • 27
0

You can use PHP_EOL to have a cross-system script working on more systems... but even if it's useful sometime you can find this constant undefined, modern hosting with latest php engine do not have this problem but I think that placing this code in top of your script will save your time spent:

<?php
  if (!defined('PHP_EOL')) {
    if (strtoupper(substr(PHP_OS,0,3) == 'WIN')) {
      define('PHP_EOL',"\r\n");
    } elseif (strtoupper(substr(PHP_OS,0,3) == 'MAC')) {
      define('PHP_EOL',"\r");
    } elseif (strtoupper(substr(PHP_OS,0,3) == 'DAR')) {
      define('PHP_EOL',"\n");
    } else {
      define('PHP_EOL',"\n");
    }
  }
?>

So you can use PHP_EOL without problems... obvious that PHP_EOL should be used on script that should work on more systems that is equivalent to \n or \r or \r\n...

Note about PHP_EOL:

1) on Unix    LN    == \n
2) on Mac     CR    == \r
3) on Windows CR+LN == \r\n

Hope this answer help.

Alessandro
  • 900
  • 12
  • 23