Page 1 of 1

calling line from text file, won't display past & ampersand?

Posted: Mon Jul 12, 2010 11:19 am
by shanehunter
Hey Guys,

So I have this code, it takes a line from a text file and uses that line as part of a post that it is scheduled to make via cron job to a website.

The code is ALMOST doing it's job - except for one thing - if the line contains an ampersand "&" then the line 'ends' there when brought back to the code.

ie:

http://www.billybobssite.com/user/profile=jmike&19428

becomes

http://www.billybobssite.com/user/profile=jmike

stripping the

&19428

Two things:

1. Why is it doing this?
2. How do I fix it?

=)

Code: Select all

function NextLine() {
        $textfile = "links.txt";
        if(file_exists($textfile)){
                $sites = file($textfile, FILE_SKIP_EMPTY_LINES);
                if(!empty($sites)) {
                        $string = array_shift($sites);
                        file_put_contents($textfile, implode($sites));
                } else {
                        $string = "Empty";
                }              
        } else {
                $string = "Error";
        };
        return $string;
}

$s0 = NextLine();

echo "$s0";

Re: calling line from text file, won't display past & ampers

Posted: Mon Jul 12, 2010 12:08 pm
by shanehunter
hey guys,

please help me out with this one.

Re: calling line from text file, won't display past & ampers

Posted: Mon Jul 12, 2010 12:11 pm
by Jade
You need to encode the string.

Code: Select all

function NextLine() {
        $textfile = "links.txt";
        if(file_exists($textfile)){
                $sites = file($textfile, FILE_SKIP_EMPTY_LINES);
                if(!empty($sites)) {
                        $string = array_shift($sites);
                        file_put_contents($textfile, implode($sites));
                } else {
                        $string = "Empty";
                }              
        } else {
                $string = "Error";
        };
        return urlencode($string);
}

$s0 = NextLine();

echo "$s0";

Re: calling line from text file, won't display past & ampers

Posted: Mon Jul 12, 2010 12:12 pm
by shanehunter
Jade,

Thank you! =)

As always, you prove to be VERY helpful to a PHP newb who's trying his hardest! =)

Re: calling line from text file, won't display past & ampers

Posted: Mon Jul 12, 2010 12:14 pm
by Jade
Haha well I've been there and done that so I completely understand the frustration.