Forum Moderators: coopster
<html>
<head>
<title> results </title>
<body>
<?php
$filename ="data.txt";
$myFile = fopen($filename, "r");
$fcontents = file($filename);
while (list ($line_num, $line) = each ($fcontents))
{
if (preg_match ("/name:/",$line))
{
$chars=preg_split("/:/",$line,-1,PREG_SPLIT_NO_EMPTY);
$name=$chars[1];
rtrim($name, "\n");
print "<h1>".$name."</h1><br>";
}
}
The data.txt file contains many lines including some with the format.
name:apersonsname
in them. There is a return code staying on the end of the name. For HTML this does not matter but I need to make the
name ito a filename and in the example this code is cut down from I am getting name?.extension type files where the
? seems to be a CR. (on a Free BSD server that is.)
This is part of the output from the code above.You can see the CRs added after every name. I have tried rtrim, trim and chop with and without the "\n" parameter. Nothing works
can anyone tell me what is wrong.
<html>
<head>
<title> results </title>
<body>
<h1>Kristy
</h1><br><h1>Valerie
</h1><br><h1>Blanche
rtrim($name);
As you have already tried that, I would say there is something fishy going on in your text file or server configuration. Try looking into the auto_detect_line_endings [us3.php.net]
<This is part of the output from the code above.You can see the CRs added after every name. I have tried rtrim, trim and chop with and without the "\n" parameter.>
Looks like the right answer has probably already been posted, but just wanted to clear something else up as well. "\n" is a newline, not a carriage return (CR). "\r" is a carriage return. So if you are specifically trying to remove carriage returns only, you're not going to want to specify "\n" :-).