This should be fairly straightforward. Say I have the following code:
$output = file_get_contents($random_name . ".txt");
echo "<pre>";
echo str_replace("\n", "\nN ", $output);
echo "</pre>";
And $output
looks like this:
PDF Test File
N Congratulations, your computer is equipped with a PDF (Portable Document Format)
N reader! You should be able to view any of the PDF documents and forms available on
N our site. PDF forms are indicated by these icons:
N or.
N
N
And let's say I want to get rid of those two last newline characters, through the following:
$outputTrimmed = trim($output, "\n");
I would assume, that would output:
PDF Test File
N Congratulations, your computer is equipped with a PDF (Portable Document Format)
N reader! You should be able to view any of the PDF documents and forms available on
N our site. PDF forms are indicated by these icons:
N or.
But instead, this code:
$output = file_get_contents($random_name . ".txt");
$outputTrimmed = trim($output, "\n");
echo "<pre>";
echo str_replace("\n", "\nN ", $outputTrimmed);
echo "</pre>";
Results in:
PDF Test File
N Congratulations, your computer is equipped with a PDF (Portable Document Format)
N reader! You should be able to view any of the PDF documents and forms available on
N our site. PDF forms are indicated by these icons:
N or.
N
N
What am I doing wrong? It's probably something really, really simple... so I apologize.
You are probably using Windows End-of-line style.
Which is \r\n
, not just \n
.
Try replacing both.
OR, don't specify any charlist (2nd parameter). By specifying \n
you are saying ONLY trim \n
trim($output)
See the docs here: http://www.w3schools.com/php/func_string_trim.asp#gsc.tab=0
EDIT (from your comments):
If trim() is not working try changing your string to a byte array and examining exactly what character is at the end of your string. This is making me suspect there's some other non-printable character interfering.
$byteArray = unpack('C*', $output);
var_dump($byteArray);
http://www.asciitable.com/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With