homepage Welcome to WebmasterWorld Guest from 54.167.182.201
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

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

10+ Year Member



 
Msg#: 1936 posted 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

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



 
Msg#: 1936 posted 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

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



 
Msg#: 1936 posted 10:39 pm on Oct 27, 2003 (gmt 0)

did you try

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

it returns a string

coopster

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



 
Msg#: 1936 posted 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

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



 
Msg#: 1936 posted 10:47 pm on Oct 27, 2003 (gmt 0)

we've all done it ;)

coopster

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



 
Msg#: 1936 posted 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

10+ Year Member



 
Msg#: 1936 posted 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

10+ Year Member



 
Msg#: 1936 posted 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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved