|Trim not working. why?|
I need to get varibles without? in them
| 8:28 pm on Oct 27, 2003 (gmt 0)|
I am having trouble getting a trim statement to work.
What I am looking for is an equivalent to Perl's Chomp.
<title> results </title>
$myFile = fopen($filename, "r");
$fcontents = file($filename);
while (list ($line_num, $line) = each ($fcontents))
if (preg_match ("/name:/",$line))
The data.txt file contains many lines including some with the format.
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.
<title> results </title>
| 9:39 pm on Oct 27, 2003 (gmt 0)|
Just leave the second optional parameter (\n) off. rtrim [us3.php.net] will strip that off by default.
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]
| 10:39 pm on Oct 27, 2003 (gmt 0)|
did you try
$name = rtrim($name);
it returns a string
| 10:46 pm on Oct 27, 2003 (gmt 0)|
Good catch, jatar_k. I've done this before myself! Trimmed without assigning to a variable. Nice detective work ;)
| 10:47 pm on Oct 27, 2003 (gmt 0)|
we've all done it ;)
| 10:55 pm on Oct 27, 2003 (gmt 0)|
Yeah, I'm sure. Bouncing over from Perl, I made the same mistake as neiljones, here -- chop and chomp change the original variable, but PHP doesn't do that with it's associated trim functions. Similar languages in many ways, but the little things, especially a lot of the Perl shortcuts, can throw you for a loop!
| 6:27 am on Oct 28, 2003 (gmt 0)|
<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" :-).
| 9:11 am on Oct 28, 2003 (gmt 0)|
Thanks everybody. It is obvious when it is pointed out.
I shall watch out for this kind of thing in future.
I knew about /r /n etc. I'd tried that too.
Similar languages but different enough to cause problems.