Jump to content

Searching a Tab Deliminated Text File (I dont even know where to start)


Recommended Posts

OK, I am trying to make a program to read a text file with the following format:

 

FILEID\tFILENAME\r\n
FILEID\tFILENAME\r\n

 

I know to use split("\t",$string); to split the fileid from the name, but still how would I go abouts reading this file and looking for the line that has the name?

 

I though of using eregi but then I though "hmm, that would be incredibly server intensive"

so what do you think I can or should do?

 

try get all the names and id's into a array

$arr = array( "fileid" => array(),

                  "fname" => array());

$arr[fileid][0] = "fileid1";

$arr[fname][0] = "filename1";

 

use a for loop to search it, i think that would be the fastest

 

not exactly sure what you mean...

 

I have hte file contents stored in STRING

 

CURRENT SEARCH CODE:

<?PHP
$string = file_get_contents(./Files/file.lst');



?>

 

FILE.LST:

7504434428	*.exe
7513176882	*.bat
7562112438	*.rar
7575651003	*.zip

 

I replaced the names with stars, and NO we do not have 75billion uploads, its a rand number.

 

how would I change that to array?

If you want to search for the id:

<?php
$h = NULL;
$cols = array();
$line = "";

if(($h = @fopen('file.txt', 'rb')) === FALSE)
    die('Failed to open some file');

while(($line = @fgets($h)) !== FALSE)
{
    $cols = explode('\t', $h);
    /* $cols[0] is the fileid */
    /* $cols[1] is the filename */
}


fclose($h);
?>

Search or do whatever. :)

ok dudes, well, I am actually loading this text file cause im gonna allow hte user to do a PARTIAL and WILDCARD search on the FILENAME's. I already have one where they type part of the ID and it goes to it. I am making a fulltext search of the filenames... Would you be able to help me with that? lol. (I shoulda clarified)

 

The above SRC would work, but how would I search $cols[1] ? there is no find or search command in php...

preg_match ftw.

 

<?php
$fileid = "/^([0-9]{1,20}).*$/";
$filename = "/([a-z0-9_.]{1,255})$/";
//first find the file id
echo "File id's:";
foreach(file('filename.txt') as $haystack) {
preg_match($fileid, $haystack, $matches);
print_r($matches);
}
echo "<br/>";
echo "File Names: ";

//then find the file name
foreach(file('filename.txt') as $haystack) {
preg_match($filename, $haystack, $matches);
print_r($matches);
}
?>

 

The array shows you can print the value of matches by using $matches[1]. So to answer your question:

echo $matches[1]; //put this in place of print_r($matches)

ok im trying it out performance wise riight now, I just ran athe script with a loop of 8000 to create 8000 diferent file id's and names, im going to search them now with the above script and time it.  Thanks there guys for helping me out! woot!

 

PREG_MATCH FTW!

<?php

$fileid = "/^([0-9]{1,20}).*$/";
$filename = "/([a-z0-9_.]{1,255})$/";

echo "File id's:";

foreach(file('./Files/file.lst') as $haystack) {
preg_match($fileid, $haystack, $matches);
print_r($matches);
}
echo "<br/>\r\n<br/>\r\nFile Names: ";

foreach(file('./Files/file.lst') as $haystack) {
preg_match($filename, $haystack, $matches);
print_r($matches);
}

?>

 

The only problem with this code, is... Where do I feed in the $_GET or $_POST search terms?  I understand preg_match but should I then replace the preg term with the search term!?  (I noticed it was regular expression but I could do string matching because it is word searching, that would be way faster wouldn't it?  *without the preg engine*)  I am not sure, but I am overlooking the $NEEDLE...

ok, I have made ascript to split the numbers (id) from the names (files)...

 

CODE:

<?php

$string = file_get_contents('./Files/file.lst');

foreach(file('./Files/file.lst') as $haystack) {
list($ID[], $FI[]) = split("[\t]", $haystack);
}

echo '<PRE>';
print_r($ID);
print_r($FI);
echo '</PRE>';
?>

 

THIS OUTPUTS:

Array
(
   [0] => 7504434428
   [1] => 7513176882
   [2] => 7562112438
   [3] => 7575651003
   [4] => 7081649567
   [5] => 7190396421
   [6] => 7233149017
   [7] => 7234947174
   [8] => 7240136136
   [9] => 7241585907
   [10] => 7251364519
)
Array
(
   [0] => *.exe

   [1] => *.bat

   [2] => *.bat

   [3] => *.bat

   [4] => 

   [5] => *.gif

   [6] => *.exe

   [7] => *.exe

   [8] => *.exe

   [9] => *.exe

   [10] => *.exe
)

 

I replaced the filenames with * but you get the point, now could I loop thorugh the array saying foreach ($ID => $FI as $FIID && $FILE) ??  (HELP PLZ)

OK so please help me!!! My current source is:

 

<PRE>
<?php

$string = file_get_contents('./Files/file.lst');
$MATCH = 'a';

foreach(file('./Files/file.lst') as $haystack) {
$ID[] = str_replace("\r\n",'',split("[\t]", $haystack));
}

foreach ($ID as $IDNM => $FIL) {
  foreach ($FIL as $FILE) {
    if (eregi($MATCH, $FILE)) {$R++;
      $RET.="<TR><TD>$FILE</TD><TD>ID: $IDNM</TD></TR>\r\n";

      $IDFND[$FILE] = $IDNM;

      foreach ($FIL as $IDS) {
        if ($d=1) {
          $Z.= $IDS."{}"; $d++;
        } else if ($d=2) {
          $Z.= $IDS."\r\n"; $d=1;
        }
      }
    }
  }
}
echo $Z;
print_r($IDFND);
print_r($ID);

echo $string;

echo "<div align=\"center\">$R Results Returned:\r\n\r\n<br/>";
echo "<br/><TABLE WIDTH=\"50%\">".$RET.'</TABLE></div>';

?>
</PRE>

 

The current page output is:

<PRE>
7504434428{}Gmod Auto Updater.exe{}7513176882{}asdf.bat{}7562112438{}asdf.bat{}7575651003{}asdf.bat{}7190396421{}BANNER-bonw.gif{}7233149017{}Garry Newman's Modification (10) Addon Package.exe{}7234947174{}Garry Newman's Modification (10) Addon Package.exe{}7240136136{}Garry Newman's Modification (10) Addon Package.exe{}7241585907{}Garry Newman's Modification (10) Addon Package.exe{}7251364519{}Garry Newman's Modification (10) Addon Package.exe{}Array
(
    [Gmod Auto Updater.exe] => 0
    [asdf.bat] => 3
    [bANNER-bonw.gif] => 5
    [Garry Newman's Modification (10) Addon Package.exe] => 10
)
Array
(
    [0] => Array
        (
            [0] => 7504434428
            [1] => Gmod Auto Updater.exe
        )

    [1] => Array
        (
            [0] => 7513176882
            [1] => asdf.bat
        )

    [2] => Array
        (
            [0] => 7562112438
            [1] => asdf.bat
        )

    [3] => Array
        (
            [0] => 7575651003
            [1] => asdf.bat
        )

    [4] => Array
        (
            [0] => 7081649567
            [1] => 
        )

    [5] => Array
        (
            [0] => 7190396421
            [1] => BANNER-bonw.gif
        )

    [6] => Array
        (
            [0] => 7233149017
            [1] => Garry Newman's Modification (10) Addon Package.exe
        )

    [7] => Array
        (
            [0] => 7234947174
            [1] => Garry Newman's Modification (10) Addon Package.exe
        )

    [8] => Array
        (
            [0] => 7240136136
            [1] => Garry Newman's Modification (10) Addon Package.exe
        )

    [9] => Array
        (
            [0] => 7241585907
            [1] => Garry Newman's Modification (10) Addon Package.exe
        )

    [10] => Array
        (
            [0] => 7251364519
            [1] => Garry Newman's Modification (10) Addon Package.exe
        )

)
7504434428	Gmod Auto Updater.exe
7513176882	asdf.bat
7562112438	asdf.bat
7575651003	asdf.bat
7081649567	
7190396421	BANNER-bonw.gif
7233149017	Garry Newman's Modification (10) Addon Package.exe
7234947174	Garry Newman's Modification (10) Addon Package.exe
7240136136	Garry Newman's Modification (10) Addon Package.exe
7241585907	Garry Newman's Modification (10) Addon Package.exe
7251364519	Garry Newman's Modification (10) Addon Package.exe<div align="center">10 Results Returned:

<br/><br/><TABLE WIDTH="50%"><TR><TD>Gmod Auto Updater.exe</TD><TD>ID: 0</TD></TR>
<TR><TD>asdf.bat</TD><TD>ID: 1</TD></TR>
<TR><TD>asdf.bat</TD><TD>ID: 2</TD></TR>
<TR><TD>asdf.bat</TD><TD>ID: 3</TD></TR>
<TR><TD>BANNER-bonw.gif</TD><TD>ID: 5</TD></TR>
<TR><TD>Garry Newman's Modification (10) Addon Package.exe</TD><TD>ID: 6</TD></TR>
<TR><TD>Garry Newman's Modification (10) Addon Package.exe</TD><TD>ID: 7</TD></TR>
<TR><TD>Garry Newman's Modification (10) Addon Package.exe</TD><TD>ID: 8</TD></TR>
<TR><TD>Garry Newman's Modification (10) Addon Package.exe</TD><TD>ID: 9</TD></TR>
<TR><TD>Garry Newman's Modification (10) Addon Package.exe</TD><TD>ID: 10</TD></TR>
</TABLE></div></PRE>

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.