warptwist Posted November 18, 2008 Share Posted November 18, 2008 I need a way to hold some data in a file and when needed, echo a speific section of the file to my site. Is there a way to do this? example: "dataholder.txt" // this is the first section I have some data here. a bit more here. // this is the seond section this data is for another page on my site. on the first page of my site I want to be able to echo the first section of "dataholder.txt", an on multiple other pages it should be the second section that gets echoed. Quote Link to comment https://forums.phpfreaks.com/topic/133167-solved-get-content-from-a-file-and-echo-to-site/ Share on other sites More sharing options...
Adam Posted November 18, 2008 Share Posted November 18, 2008 It perfectly possible, but you'd need some kind of delimiter to determine when one section starts and another begins. Can be anything really.. blah blah this is section 1 // NEW_SECTION blah blah this is section 2 Then you can use explode() to explode the contents by "// NEW_SECTION" and echo out the right section. A quick example: <?php $text = file_get_contents("dataholder.txt"); $sections = explode("// NEW_SECTION", $text); echo $sections[0]; // section 1 echo $sections[1]; // section 2 ?> Adam Quote Link to comment https://forums.phpfreaks.com/topic/133167-solved-get-content-from-a-file-and-echo-to-site/#findComment-692566 Share on other sites More sharing options...
warptwist Posted November 18, 2008 Author Share Posted November 18, 2008 Nice, Thanks Quote Link to comment https://forums.phpfreaks.com/topic/133167-solved-get-content-from-a-file-and-echo-to-site/#findComment-692570 Share on other sites More sharing options...
warptwist Posted November 18, 2008 Author Share Posted November 18, 2008 The way it shows up on my site is in 1 continous string. Can I get it to show exactly as written in the txt file (linebreaks), so a multiline text in the txt file would show up as multiline on the page. Quote Link to comment https://forums.phpfreaks.com/topic/133167-solved-get-content-from-a-file-and-echo-to-site/#findComment-692600 Share on other sites More sharing options...
Adam Posted November 18, 2008 Share Posted November 18, 2008 Yeah use: $text = str_replace("\n", "<br />", $text); Quote Link to comment https://forums.phpfreaks.com/topic/133167-solved-get-content-from-a-file-and-echo-to-site/#findComment-692606 Share on other sites More sharing options...
warptwist Posted November 18, 2008 Author Share Posted November 18, 2008 again thx MrAdam. Quote Link to comment https://forums.phpfreaks.com/topic/133167-solved-get-content-from-a-file-and-echo-to-site/#findComment-692608 Share on other sites More sharing options...
JasonLewis Posted November 18, 2008 Share Posted November 18, 2008 Have a look at nl2br. Quote Link to comment https://forums.phpfreaks.com/topic/133167-solved-get-content-from-a-file-and-echo-to-site/#findComment-692613 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.