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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shanehunter
Forum Commoner
Posts: 30
Joined: Sun Jun 27, 2010 3:43 pm

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

Post 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";
shanehunter
Forum Commoner
Posts: 30
Joined: Sun Jun 27, 2010 3:43 pm

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

Post by shanehunter »

hey guys,

please help me out with this one.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

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

Post 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";
shanehunter
Forum Commoner
Posts: 30
Joined: Sun Jun 27, 2010 3:43 pm

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

Post by shanehunter »

Jade,

Thank you! =)

As always, you prove to be VERY helpful to a PHP newb who's trying his hardest! =)
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

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

Post by Jade »

Haha well I've been there and done that so I completely understand the frustration.
Post Reply