Reputation: 11777
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.
Upvotes: 4
Views: 3255
Reputation: 7909
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);
Upvotes: 7
Reputation: 1617
try this
$output1 = file_get_contents($random_name . ".txt");
$output=str_replace("\n", "\nN ", $output1);
$outputTrimmed = trim($output,"\n");
echo "<pre>";
echo $outputTrimmed;
echo "<pre>";
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 .
Upvotes: 2