| Trim not working. why? I need to get varibles without? in them |
neiljones

msg:1280325 | 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. <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
|
coopster

msg:1280326 | 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.
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]
|
jatar_k

msg:1280327 | 10:39 pm on Oct 27, 2003 (gmt 0) | did you try $name = rtrim($name); echo $name; it returns a string
|
coopster

msg:1280328 | 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 ;)
|
jatar_k

msg:1280329 | 10:47 pm on Oct 27, 2003 (gmt 0) | we've all done it ;)
|
coopster

msg:1280330 | 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!
|
NickCoons

msg:1280331 | 6:27 am on Oct 28, 2003 (gmt 0) | neiljones, <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" :-).
|
neiljones

msg:1280332 | 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.
|
|
|