Reading Text File

Hi All,
I have a Text File, which has Data as follows:
<Name>,<Phone>,<eMail>
Jhon Doe, 111-111-1111, jdoe@yahoo.com

I have a PHP script that reads data from text file, which works fine but it does not display the PHONE field, because it has DASH in them; can anyone tell me how to solve that?

Here is my PHP Script:

<?php 
$content = file("TXT.txt");

for($i=0; $i<count($content); $i++) 
{ 
    $single = explode(",", $content[$i]); 
    ?> 
    <tr> 
    <?php 
    for($j=0; $j<count($single); $j++) 
    { 
        ?> 
        <td align="center"><?php echo $single[$j];?></td> 
        <?php 
    } 
    ?> 
    </tr> 
    <?php 
} 
?>

What? Why wouldn’t your code display dashes?

Can you post the final html code outputted by your php code? Just rightclick in your browser window, select ‘html code’ or ‘source code’ or whatever, copy the code and post it here.

Hello Guido2004: Weird Thing, perhaps, I can tell you two weird things.
One, when I view my source code as you suggested, I see the phone number as listed in the code below; but on my page I can not see the phone number.

Second, when I refresh the page (F5), I see the phone number for a split-second then it disappears. Can not figureout why?

I am running script on Local Server WAMP.


<!-- Read All Records -->
 
<div align="center"> 
<table border="0" cellpadding="0" cellspacing="4" width="800"> 
<tr> 
  <td align="center" bgcolor="#E6E6E6">NAME</td> 
  <td align="center" bgcolor="#E6E6E6">PHONE</td> 
  <td align="center" bgcolor="#E6E6E6">eMAIL</td> 
</tr> 
 
<tr> 
  <td align="center"><Name></td> 
  <td align="center"><Phone></td> 
  <td align="center"><eMail></td> 
</tr> 
     
<tr> 
   <td align="center">Jhon Doe</td> 
   <td align="center"> 111-111-1111</td> 
   <td align="center"> jdoe@yahoo.com</td> 
</tr> 

Strange. Is it just the number that disappears? Or is it the whole column (including the header)?

Yeah, its just the number that disappears. I tell you another strange thing.

If I enter phone number 111-111-1111, it works. But when I change area code to 408 then it disappears???

Seems like something is screwed up, specially in my script.

Does anyone think there is anything wrong with the Number? Area Code 108 and 200 works? 201 does not work.

You have produced invalid html. Once you do that, you should expect browsers to do all sorts of funky things.


  <td align="center"><Name></td> 
  <td align="center"><Phone></td> 
  <td align="center"><eMail></td>

There is no <Name> tag in html. If you want to visually display ‘<Name>’, then run the values through htmlspecialchars(). In fact, any value you output into an html document should be run through that function, for correctness and security reasons.

Thanks Crmalibu,

I must admit that I am using readily available script (showed in my Top Post), and it displays all the data from Text file, but I am a newoob to understand how one part of code may affect the entire program.

May be that is why I did not understand your reply.

Like this


<?php 
$content = file("TXT.txt");

for($i=0; $i<count($content); $i++) 
{ 
    $single = explode(",", $content[$i]); 
    ?> 
    <tr> 
    <?php 
    for($j=0; $j<count($single); $j++) 
    { 
        ?> 
        <td align="center"><?php echo htmlspecialchars($single[$j]);?></td> 
        <?php 
    } 
    ?> 
    </tr> 
    <?php 
} 
?>

Thanks Crmalibu, I did try that but it did not work; which is really strange. Here I have broke down the my Data file (TXT.txt) & Code file completely.

My script reads Data file and prints first two lines OK, but in third line it does not print Phone number; strange thing is it will print phone number if Area Code is less then 200.

Can anyone run this script and see if they see the same problem??? Thanks.

My Data-file: TXT.txt
John Doe, 111-111-1111, jdoe@yahoo.com
Mary Joe, 222-222-2222, mjoe@yahoo.com
Gung Hoe, 156 Cripto Circle, Rougeville, NJ., 37588, 301-288-1342, ghoe@yahoo.com


[B]My PHP SCRIPT:[/B]
<table align="center" cellspacing="4" width="800"> 

<?php 
$content = file("TXT.txt");

for($i=0; $i<count($content); $i++) 
{ 
    $single = explode(",", $content[$i]); 
    echo "<tr>";
	 
    for($j=0; $j<count($single); $j++) 
    { 
        echo "<td align=\\"center\\">";
		echo $single[$j];
		echo "</td>"; 
    } 
    echo "</tr>"; 
}

My Output: No Phone Number on third line…
John Doe 111-111-1111 jdoe@yahoo.com
Mary Joe 222-222-2222 mjoe@yahoo.com
Gung Hoe 156 Cripto Circle Rougeville NJ. 37588 ghoe@yahoo.com

It works just fine for me. But what are those [B] tags in the first line? Do you really have them in your code? Do you use a template engine or something?

Hello guido2004, you said,

But what are those [B] tags in the first line?

No, I just added first line to tell everyone that they are my PHP Code script.

But in any case, thanks so much for trying. Appreciate it very much.

Thanks Guido2004, I think this issue is solved.

Everything works on my Website, for some reason it does not work only on my Local Server, specially with Internet Explorer.