473,320 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Code needed to make PHP read from text file Properly

6
Hello,
I am new to php, and I need some help.
I am trying to write a script for a web site.
Text I need to display is in a .txt file, and I can't add breaks to it.
I wrote a function that will open my files. When text is displaid in html, it is all in one line. I can not find code to add my function to take care of the problem.
can sombody please help?
Thank you
Jul 19 '07 #1
10 2250
ak1dnar
1,584 Expert 1GB
  • How is your text file contents apperaing?
  • Where is the code that you made so far?
Please post them here!
Jul 19 '07 #2
mwasif
802 Expert 512MB
Text I need to display is in a .txt file, and I can't add breaks to it.
Please explain.

Use nl2br() for line breaks.
Jul 19 '07 #3
kvazar
6
Here is function code I wrote

<?php
function open_question($category) {

$file = fopen($category, 'r') or die("Can't open file");
$theData = fread($file, 6800);
fclose($file);
echo $theData;
}
?>

<html>
<body>
<?php
open_question("./text.txt");{
?>
</body>
</html>


text.txt just has a bunch of text in it, nothing significant.
for example
text.txt hassuch text in it:


I
need
help

And this is exsactly how I want it to show on my site.
But instead it will all be in one line, like "I need help", and I don't what that!

Hope this explanation helps
Jul 19 '07 #4
mwasif
802 Expert 512MB
Use nl2br().
[PHP]<?php
nl2br(open_question("./text.txt"));
?>[/PHP]

Where you want to print the braces?
Jul 19 '07 #5
ak1dnar
1,584 Expert 1GB
Still your Question is not clear to me.anyway is this the thing you need.?If not please post back.

php script
Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. function open_question($category) {
  3. $file = fopen($category, 'r') or die("Can't open file");
  4. $theData = fread($file, 6800);
  5. fclose($file);
  6. $delimiter = "\n";
  7. $splitcontents = explode($delimiter, $theData);
  8. foreach( $splitcontents as $record )
  9. {
  10. echo $record.'<br>';
  11. }
  12. open_question("./text.txt");
  13. ?>
  14.  
text.txt
Expand|Select|Wrap|Line Numbers
  1. I
  2. need sdsds oklsdkdkskfldsf
  3. help
  4. ldjkfgdlkglkdflgl
Jul 19 '07 #6
kvazar
6
Yes!!! That has worked perfectly.
I am sorry for being a bit unclear with my questions. But yes! That is exsactly what I needed.

Now, is there also a way for me to add code that will for esample omit line 3.help?

thanks
Jul 19 '07 #7
ak1dnar
1,584 Expert 1GB
Yes!!! That has worked perfectly.
I am sorry for being a bit unclear with my questions. But yes! That is exsactly what I needed.

Now, is there also a way for me to add code that will for esample omit line 3.help?

thanks
Do you want to omit just only line number 3 ?
Is there any other text input after 3rd line in your original text file.
Jul 19 '07 #8
kvazar
6
Ok, here is an exsample of something like I would use for my text file.

QUESTION
BLAH BLAH BLAH BLAH BLAH BLAH BLAH
FIGURE
None
OPTIONS
1
10
100
30
40
ANSWER
D


All of this information that you see has, and will be in the text file. Though I do not need to display all of it.
I would need to get rid of lines QUESTION, FIGURE, None, and of course i do not want my ANSWER line with an answer to show.
How do I get rid of that.
As I have mentioned, all this has to be in my text file.
Thanks
Jul 19 '07 #9
kvazar
6
Would anyone have any idea on how I could posibly do this, or if it is possible at all.
Thanks
Jul 19 '07 #10
tscott
22
Try using Explode to break it up into an array via line breaks.Then you can only echo the lines you want

example (using spaces)
[php]
<?php
$getfile = file_get_contents('textfile.txt');
$filearray = explode(' ' , $getfile);
echo $filearray[0] . ' ' . $filearray[4] , ' ' . $filearray[2];
// Say the contents is:I random pie dog like//
//This would say: I like pie//
?>
[/php]
Jul 19 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

53
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is...
6
by: Mario T. Lanza | last post by:
Greetings, I don't know about you guys but on many occasions I've asked myself whether or not someone else has solved a particular programming issue -- whether or not they developed a clever...
67
by: Steven T. Hatton | last post by:
Some people have suggested the desire for code completion and refined edit-time error detection are an indication of incompetence on the part of the programmer who wants such features. ...
18
by: Dixie | last post by:
Can I set the Format property in a date/time field in code? Can I set the Input Mask in a date/time field in code? Can I set the Format of a Yes/No field to Checkbox in code? I am working on...
6
by: Paolo Pignatelli | last post by:
I have an aspx code behind page that goes something like this in the HTML view: <asp:HyperLink id=HyperLink1 runat="server" NavigateUrl='<%#"mailto:" &amp;...
17
by: tshad | last post by:
Many (if not most) have said that code-behind is best if working in teams - which does seem logical. How do you deal with the flow of the work? I have someone who is good at designing, but...
7
by: Alan Silver | last post by:
Hello, I have installed the 2.0 framework, and am looking at converting some of my 1.1 pages to use partial classes. I don't (yet) have VS2005, so I'm doing this by hand, but am having problems....
2
by: JCC | last post by:
Hi all Could someone help me with the following problem? Hours of googling yesterday couldn't get me the answer. I'm using SQL 2000 and DTS and trying to import a huge fixed width text file....
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.