Jump to content

A question about using a text file as a PHP Array.


cuboidgraphix

Recommended Posts

Hi,

Quick question...  Can a text file with a bunch of numbers be used as an array for PHP? The thing is that I want to use this php script.

 

<?php
$HLR = array('A','B','H','J','K','L','M','P','T');
$VLR = array('A','B','L','T');

$Difference = array_diff($HLR, $VLR);

print_r($Difference);

echo '<br /><br />' . implode(', ', $Difference); 
?>

 

But instead of 'A','B', etc...  I want to use a text file  HLR.txt.

Inside this text file I'll have numbers like... 

6701001

6701231

6701343 .... and so on.  thousands of numbers. Each line is a different number.

 

So in conclusion, can a text file with a whole bunch of numbers be converted into an array or read as an array for PHP?

 

Thanks..  Awaiting patiently for your replies.

Link to comment
Share on other sites

Daniel0

 

Thanks, but I don't think you quite understood what I was looking for.  The PHP Script I have there isn't exactly what I'm using. the arrays are in a text file. eg: HLR.txt  and VLR.txt .

 

I'm trying to find out if those two text files can be called to the php script and USED as arrays.

 

In lame man's terms.  I have two text files in my Hard Drive. I want to use a php page that will upload the two text files using a form, and run the script.  My question is ... is this possible?  Using the Two text files as arrays?  And if so, how ... through a different script?

Link to comment
Share on other sites

OK guys, I'm not sure how much damage I've caused, but this is what I have.  It ain't working for sh*t.  :D

 

<!-- Form Start -->  
<form name="form" method="POST" action="<?=$PHP_SELF?>">
Get Difference Between: 
HLR <input name="hlr" type="file" id="hlrfile" size="20" /> and 
VLR <input name="vlr" type="file" id="vlrfile" size="20" />
<input type="submit" name="execute" value="Execute" />
</form>                        
<?php
$h = @$_GET['hlr'];
$v = @$_GET['vlr'];	
$hl = fopen("$h", "r");
$vl = fopen("$v", "r");
$hlr = str_replace("\n", ",", $hl);
$vlr = str_replace("\n", ",", $vl);		
$harray = array("$hlr");
$varray = array("$vlr");

$diff = array_diff($harray, $varray);			
print_r($diff);			
//echo '<br />' . implode(', ', $diff); 
?>

 

Am I on the right track?

Link to comment
Share on other sites

Well what I really wanted is to browse in my hard drive, and not really upload it but just run the script on those two files.   Do I need to upload them first?

 

yes. PHP is a server-side language, so for it to analyze a file on your hard drive you must upload it to the server. It might be possible for PHP to connect to your server and retrieve the file itself, but you don't want to go there. you want to upload the files and have PHP compare them on the server.

 

Link to comment
Share on other sites

this works for me:

 

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$hrname = 'hlrfile.txt';
$vrname = 'vlrfile.txt';

copy($HTTP_POST_FILES["hlr"]['tmp_name'],"./$hrname") or die("failed to copy hlr");
copy($HTTP_POST_FILES["vlr"]['tmp_name'],"./$vrname") or die("failed to copy vlr");

$hrarray = file($hrname);
$vrarray = file($vrname);

str_replace("\n","",$hrarray);
str_replace("\n","",$vrarray);

print_r($hrarray);
echo "<BR>";
print_r($vrarray);
}
?>
<HTML>
<BODY>
<!-- Form Start --> 
<form name="form" method="POST" action="<?=$PHP_SELF?>" enctype='multipart/form-data'>
<INPUT TYPE='hidden' NAME='MAX_FILE_SIZE' VALUE='2000000'>
Get Difference Between: 
HLR <input name="hlr" type="file" id="hlrfile" size="20" /> and 
VLR <input name="vlr" type="file" id="vlrfile" size="20" />
<input type="submit" name="execute" value="Execute" />
</BODY>
</HTML>

Link to comment
Share on other sites

Hi again guys, Well i have a code that works whenever I have numbers like.... 

 

1

2

3

4

5

6

7

8

9

10

11

etc...

 

But it does not seem to work when I have numbers such as,

 

2800001

2801000

2801010

2801011

2801012

2810123

6600000

6701050

6701052

 

Can you see if anything is wrong with my code?

 

<?php
// Array Difference Script
$hlr = @fopen('hlr.txt', 'r');
if ($hlr){
$array1 = explode("\n", fread($hlr, filesize("hlr.txt")));
} 

$vlr = @fopen('vlr.txt', 'r');
if ($vlr){
$array2 = explode("\n", fread($vlr, filesize("vlr.txt")));
}

$diff = array_diff($array1, $array2);
foreach ($diff as $value){
  echo $value . " 
";
} 	
?> 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.