Jump to content

Archived

This topic is now archived and is closed to further replies.

Roelandje

Pick random sentence from file.

Recommended Posts

Hello everyone,

 

I need to figure out how i can randomly echo a sentence from a file.

I have this piece of code, wich i didnt write myself, but what should work:

 

$zinnen =@file("../data/language.txt");
        if ($zinnen)
            $zin = htmlentities($zinnen[mt_rand(0, count($zinnen)-1)]);

 

 

Then if i Echo $zin, It should give me one sentence from the file 'language.txt'.

Does any of you have a clue of how i should compose that 'language.txt' file?

I need a small example.

 

Thanks in advance.

Roeland

Share this post


Link to post
Share on other sites

Hey Requinix,

 

Do you mean like just a sentences without any type of code in it?

Because I tried that and it didn't work. 

 

Share this post


Link to post
Share on other sites

data/language.txt should be a plain text file with one sentence per line.

Share this post


Link to post
Share on other sites

Do you mean like just a sentences without any type of code in it?

Yes.

Because I tried that and it didn't work.

Then something else is wrong.

Share this post


Link to post
Share on other sites

Awesome!

Thanks guys, I got it to work.

 

Does any of you happen to know if this same procedure would work for html documents?

 

Share this post


Link to post
Share on other sites

HTML isn't a programming language and as such cannot read files.

Share this post


Link to post
Share on other sites

What i mean is

 

I have a PHP code that reads a .txt file. It randomly picks a line and shows that.

 

Now, I wonder if there is a way i can do the same thing, but instead of showing sentences from a .txt file, showing HTML codes.

 

So, i can also randomly pick pictures and not just sentences.

 

Thanks for your reaction, tho

Share this post


Link to post
Share on other sites

The process is no different, just put each line of markup on a new line in a txt file.

 

Either that or add the markup to your content using php.

Share this post


Link to post
Share on other sites

I see,

What I've tried now:

 

I made a .txt file with the markup and the image i want to show on one line, I call it images.txt.

like so

 

<!DOCTYPE html><html><head><title>blabla</title></head><body><a href= the whole image thingy></body></html>

 

But it puts out that code in plain writing. It doesnt understand i want it to be treated as HTML instead of TXT.

 

I also tried to save the file as images.html in stead of images.txt... But still, it puts out the code in plain text.

 

This was what you ment, right? Do you happen to see what i do wrong here?

 

Thank you.

Roeland

Share this post


Link to post
Share on other sites

So you're spitting HTML to a browser which is not rendering that HTML?  Something else is going on.  View the source of the page.  Do you see valid HTML?

Share this post


Link to post
Share on other sites

If you want to show HTML then don't call htmlentities().

Share this post


Link to post
Share on other sites

If you want to show HTML then don't call htmlentities().

Oh geeze, it's been a few days...

Share this post


Link to post
Share on other sites

Also, assuming your php code is already running within a page, you don't need the doctype, <html> or <body> tags. The page your already on should already have these.

Share this post


Link to post
Share on other sites

Yes, I allready have the mark-up in the PHP.

I thought there wouldnt be any harm in trying to put the mark-up in it again.

Anyways, It doesnt work with or without the markup, It just puts out the code in plain writing.

 

Still haven't it figured out.

Share this post


Link to post
Share on other sites

Did you notice that requinix solved that problem on Friday?

Share this post


Link to post
Share on other sites

He says i shouldnt use htmlentities() for it.

That pin-points the problem, but doesnt solve it for me.

 

Do you happen to know what i should use instead of htmlentities()?

Share this post


Link to post
Share on other sites

Awesome...

Things are sometimes easier then I expect them to be.

Thanks for the help.

and thanks Requinix

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.