Roelandje Posted December 1, 2011 Share Posted December 1, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/252228-pick-random-sentence-from-file/ Share on other sites More sharing options...
requinix Posted December 1, 2011 Share Posted December 1, 2011 One sentence per line. Quote Link to comment https://forums.phpfreaks.com/topic/252228-pick-random-sentence-from-file/#findComment-1293193 Share on other sites More sharing options...
Roelandje Posted December 1, 2011 Author Share Posted December 1, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252228-pick-random-sentence-from-file/#findComment-1293198 Share on other sites More sharing options...
ManiacDan Posted December 1, 2011 Share Posted December 1, 2011 data/language.txt should be a plain text file with one sentence per line. Quote Link to comment https://forums.phpfreaks.com/topic/252228-pick-random-sentence-from-file/#findComment-1293229 Share on other sites More sharing options...
requinix Posted December 1, 2011 Share Posted December 1, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252228-pick-random-sentence-from-file/#findComment-1293251 Share on other sites More sharing options...
Roelandje Posted December 2, 2011 Author Share Posted December 2, 2011 Awesome! Thanks guys, I got it to work. Does any of you happen to know if this same procedure would work for html documents? Quote Link to comment https://forums.phpfreaks.com/topic/252228-pick-random-sentence-from-file/#findComment-1293323 Share on other sites More sharing options...
trq Posted December 2, 2011 Share Posted December 2, 2011 HTML isn't a programming language and as such cannot read files. Quote Link to comment https://forums.phpfreaks.com/topic/252228-pick-random-sentence-from-file/#findComment-1293326 Share on other sites More sharing options...
Roelandje Posted December 2, 2011 Author Share Posted December 2, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/252228-pick-random-sentence-from-file/#findComment-1293327 Share on other sites More sharing options...
trq Posted December 2, 2011 Share Posted December 2, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252228-pick-random-sentence-from-file/#findComment-1293382 Share on other sites More sharing options...
Roelandje Posted December 2, 2011 Author Share Posted December 2, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/252228-pick-random-sentence-from-file/#findComment-1293462 Share on other sites More sharing options...
ManiacDan Posted December 2, 2011 Share Posted December 2, 2011 are you viewing it in a browser? Quote Link to comment https://forums.phpfreaks.com/topic/252228-pick-random-sentence-from-file/#findComment-1293505 Share on other sites More sharing options...
Roelandje Posted December 2, 2011 Author Share Posted December 2, 2011 Yes I am. Quote Link to comment https://forums.phpfreaks.com/topic/252228-pick-random-sentence-from-file/#findComment-1293506 Share on other sites More sharing options...
ManiacDan Posted December 2, 2011 Share Posted December 2, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/252228-pick-random-sentence-from-file/#findComment-1293519 Share on other sites More sharing options...
requinix Posted December 2, 2011 Share Posted December 2, 2011 If you want to show HTML then don't call htmlentities(). Quote Link to comment https://forums.phpfreaks.com/topic/252228-pick-random-sentence-from-file/#findComment-1293602 Share on other sites More sharing options...
ManiacDan Posted December 2, 2011 Share Posted December 2, 2011 If you want to show HTML then don't call htmlentities(). Oh geeze, it's been a few days... Quote Link to comment https://forums.phpfreaks.com/topic/252228-pick-random-sentence-from-file/#findComment-1293612 Share on other sites More sharing options...
trq Posted December 2, 2011 Share Posted December 2, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252228-pick-random-sentence-from-file/#findComment-1293633 Share on other sites More sharing options...
Roelandje Posted December 5, 2011 Author Share Posted December 5, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252228-pick-random-sentence-from-file/#findComment-1294629 Share on other sites More sharing options...
ManiacDan Posted December 5, 2011 Share Posted December 5, 2011 Did you notice that requinix solved that problem on Friday? Quote Link to comment https://forums.phpfreaks.com/topic/252228-pick-random-sentence-from-file/#findComment-1294636 Share on other sites More sharing options...
Roelandje Posted December 5, 2011 Author Share Posted December 5, 2011 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()? Quote Link to comment https://forums.phpfreaks.com/topic/252228-pick-random-sentence-from-file/#findComment-1294642 Share on other sites More sharing options...
ManiacDan Posted December 5, 2011 Share Posted December 5, 2011 Nothing. Quote Link to comment https://forums.phpfreaks.com/topic/252228-pick-random-sentence-from-file/#findComment-1294645 Share on other sites More sharing options...
Roelandje Posted December 5, 2011 Author Share Posted December 5, 2011 Awesome... Things are sometimes easier then I expect them to be. Thanks for the help. and thanks Requinix Quote Link to comment https://forums.phpfreaks.com/topic/252228-pick-random-sentence-from-file/#findComment-1294649 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.