Welcome to WebmasterWorld Guest from 54.145.144.101

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Trim not working. why?

I need to get varibles without? in them

   
8:28 pm on Oct 27, 2003 (gmt 0)

10+ Year Member




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

9:39 pm on Oct 27, 2003 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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]

10:39 pm on Oct 27, 2003 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



did you try

$name = rtrim($name);
echo $name;

it returns a string

10:46 pm on Oct 27, 2003 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



we've all done it ;)
10:55 pm on Oct 27, 2003 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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)

10+ Year Member



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" :-).

9:11 am on Oct 28, 2003 (gmt 0)

10+ Year Member



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.