Jump to content

Taking a large txt file, searching it, and printing the results..


otchster

Recommended Posts

I need write a script that will search this file: http://cset.sp.utoledo.edu/cset3300/assignments/datafile.txt

 

The file is a class list for my school..  I need to search the file for all classes that begin with "CSET"

 

The end result should echo the results such as this:

CSET-1100 | INTRO-COMPUTER SCI & ENG TECH
CSET-1200 | GUI PROGRAMMING & VISUAL BASIC
CSET-2100 | SMALL COMPUTER SYSTEMS
CSET-2200 | PC & INDUSTRIAL NETWORKS
CSET-3100 | ADV WEB SITE DESIGN
CSET-3200 | CLIENT-SERVER COMPUTING
CSET-3250 | CLIENT-SIDE SCRIPTING
CSET-4100 | CGI PROGRAMMING W/PERL & JAVA
CSET-4150 | WEB SERVER ADMINISTRATION
CSET-4250 | APPLIED PROGRAMMING LANGUAGES
CSET-4650 | FIELD PROGRAMMABLE LOGIC DEVICES
CSET-4750 | COMPUTER NETWORKS & DATA COMM

 

I'm not 100% sure how to go about doing this.  I was going to load the file into an array, but i don't know where to go from there.. 

 

Heres where I'm at currently:

<?php

$fullname = $_POST['fullname'];
$number = $_POST['number'];


echo "<center><h3><br><hr>Question 3. </h3></center></b>";


$myFile = "http://cset.sp.utoledo.edu/cset3300/assignments/datafile.txt";

// The following reads the file into an array....

$array = file($myFile);


if (in_array("CSET", $array)) {
    echo "found CSET";
}

// The following counter would display the array...

    for ($i=0;$i<=15000;$i++)
    {
    echo $array[$i];
    echo "<br>";
    }

$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);

echo $theData;

echo "<br><hr>";

?>

 

Thanks everyone ???

Link to comment
Share on other sites

Also, don't do this, because it prints 15,000 br tags:

 

for ($i=0;$i<=15000;$i++)
    {
    echo $array[$i];
    echo "<br>";
}

 

If you want to go through each element in array, without using a foreach, do:

 

for ($i=0;$array[$i];$i++)
    {
    echo $array[$i];
    echo "<br>";
}

 

$array[$i] will evaluate to false when i is equal to the amount of elements in array.

Link to comment
Share on other sites

Boom!

 

foreach(file("http://cset.sp.utoledo.edu/cset3300/assignments/datafile.txt") as $line)
echo (substr($line, 0, 4) === "CSET" ? $line : "");

 

Solved.

 

THANK YOU!!  Now where do I insert a \n to make each result print on a new line ??

 

This:

<?php

 

$fullname = $_POST['fullname'];

$number = $_POST['number'];

 

 

echo "<center><h3><br><hr>Question 3. </h3></center></b>";

 

 

 

foreach(file("http://cset.sp.utoledo.edu/cset3300/assignments/datafile.txt") as $line)

echo (substr($line, 0, 4) === "CSET" ? $line : "");

 

echo "<br><hr>";

 

?>

 

Prints this...  :

CSET-1100 INTRO-COMPUTER SCI & ENG TECH 3 CSET-1200 GUI PROGRAMMING & VISUAL BASIC 3 CSET-2100 SMALL COMPUTER SYSTEMS 4 CSET-2200 PC & INDUSTRIAL NETWORKS 4 CSET-3100 ADV WEB SITE DESIGN 3 CSET-3200 CLIENT-SERVER COMPUTING 3 CSET-3250 CLIENT-SIDE SCRIPTING 3 CSET-4100 CGI PROGRAMMING W/PERL & JAVA 3 CSET-4150 WEB SERVER ADMINISTRATION 3 CSET-4250 APPLIED PROGRAMMING LANGUAGES 3 CSET-4650 FIELD PROGRAMMABLE LOGIC DEVICES 4 CSET-4750 COMPUTER NETWORKS & DATA COMM 4
Link to comment
Share on other sites

Thank you..  heres my final code :

<?php


echo "<center><h3><br><hr>Question 3. </h3></center></b>";



foreach(file("http://cset.sp.utoledo.edu/cset3300/assignments/datafile.txt") as $line)
echo (substr($line, 0, 4) === "CSET" ? "$line <br>" : "");



echo "<br><hr>";

?>

 

The Assignment just got a log harder though.. I need to search that same file: http://cset.sp.utoledo.edu/cset3300/assignments/datafile.txt for the CSET courses again..  But this time I need to find the 995 sections, and print the staff names.. The result should look like this: 

CSET-1200 profname2

CSET-3100 profname4

CSET-3200 profname5

CSET-3250 profname6

CSET-4100 profname7

CSET-4150 STAFF

CSET-4250 profname8

CSET-4650 profname9

CSET-4750 profname11

 

I have no problem searching the text file for CSET 995 courses, and printing the staff names..  (in this case, the names will just say "STAFF"...  But the problem Im running into is that in the text file, the Course names and the Staff names are on different lines..

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.