cuboidgraphix Posted April 16, 2008 Share Posted April 16, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/101425-a-question-about-using-a-text-file-as-a-php-array/ Share on other sites More sharing options...
Daniel0 Posted April 16, 2008 Share Posted April 16, 2008 Yes. The function file() returns an array with every line of the file having an index in the array. Quote Link to comment https://forums.phpfreaks.com/topic/101425-a-question-about-using-a-text-file-as-a-php-array/#findComment-518789 Share on other sites More sharing options...
cuboidgraphix Posted April 16, 2008 Author Share Posted April 16, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/101425-a-question-about-using-a-text-file-as-a-php-array/#findComment-518802 Share on other sites More sharing options...
Daniel0 Posted April 16, 2008 Share Posted April 16, 2008 Am I correct in believing that if the file looks like this: 6701001 6701231 6701343 then you want an array which looks like this? $array = array(6701001, 6701231, 6701343); That will turn the file into an array so to speak. Quote Link to comment https://forums.phpfreaks.com/topic/101425-a-question-about-using-a-text-file-as-a-php-array/#findComment-518810 Share on other sites More sharing options...
cuboidgraphix Posted April 16, 2008 Author Share Posted April 16, 2008 Yes. I'm sorry, I saw the code in your signature and I thought you were giving me that code. Quote Link to comment https://forums.phpfreaks.com/topic/101425-a-question-about-using-a-text-file-as-a-php-array/#findComment-518827 Share on other sites More sharing options...
Barand Posted April 16, 2008 Share Posted April 16, 2008 One thing to watch when using file() - the linefeeds aren't stripped, so the array will actually be array("6701001\n", "6701231\n", "6701343\n"); Quote Link to comment https://forums.phpfreaks.com/topic/101425-a-question-about-using-a-text-file-as-a-php-array/#findComment-518838 Share on other sites More sharing options...
jonsjava Posted April 16, 2008 Share Posted April 16, 2008 solution: str_replace("\n", "", $data); Quote Link to comment https://forums.phpfreaks.com/topic/101425-a-question-about-using-a-text-file-as-a-php-array/#findComment-518845 Share on other sites More sharing options...
cuboidgraphix Posted April 16, 2008 Author Share Posted April 16, 2008 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. <!-- 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? Quote Link to comment https://forums.phpfreaks.com/topic/101425-a-question-about-using-a-text-file-as-a-php-array/#findComment-518892 Share on other sites More sharing options...
BlueSkyIS Posted April 16, 2008 Share Posted April 16, 2008 not really. do you need to upload the files or will they definitely be located in the same directory as the script? you have a little of both in there. Quote Link to comment https://forums.phpfreaks.com/topic/101425-a-question-about-using-a-text-file-as-a-php-array/#findComment-518897 Share on other sites More sharing options...
cuboidgraphix Posted April 16, 2008 Author Share Posted April 16, 2008 hehehe. 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? Quote Link to comment https://forums.phpfreaks.com/topic/101425-a-question-about-using-a-text-file-as-a-php-array/#findComment-518906 Share on other sites More sharing options...
BlueSkyIS Posted April 16, 2008 Share Posted April 16, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/101425-a-question-about-using-a-text-file-as-a-php-array/#findComment-518908 Share on other sites More sharing options...
BlueSkyIS Posted April 16, 2008 Share Posted April 16, 2008 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> Quote Link to comment https://forums.phpfreaks.com/topic/101425-a-question-about-using-a-text-file-as-a-php-array/#findComment-518913 Share on other sites More sharing options...
cuboidgraphix Posted April 16, 2008 Author Share Posted April 16, 2008 BlueSkyIs I tried your code, and it did not work. Is there something I have to modify? Quote Link to comment https://forums.phpfreaks.com/topic/101425-a-question-about-using-a-text-file-as-a-php-array/#findComment-518927 Share on other sites More sharing options...
BlueSkyIS Posted April 16, 2008 Share Posted April 16, 2008 you need to make sure that the permissions are correct on the directory where the script resides, so that PHP is allowed to upload a file there. chmod to 755 first. if that doesn't work, try 777. Quote Link to comment https://forums.phpfreaks.com/topic/101425-a-question-about-using-a-text-file-as-a-php-array/#findComment-518944 Share on other sites More sharing options...
cuboidgraphix Posted April 24, 2008 Author Share Posted April 24, 2008 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 . " "; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/101425-a-question-about-using-a-text-file-as-a-php-array/#findComment-526169 Share on other sites More sharing options...
cuboidgraphix Posted April 24, 2008 Author Share Posted April 24, 2008 anyone? ??? Please. Quote Link to comment https://forums.phpfreaks.com/topic/101425-a-question-about-using-a-text-file-as-a-php-array/#findComment-526273 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.