Fixed txt file type issue

I'm trying to make an alert alert script for my local volunteer fire brigade, and I'm stuck.

I have a delimited txt file called preplan.txt containing lines like this:

Line1: REF00001 | NAME1 | ALERTADDRESS1 | LINK2DOWNLOADPDFINFOONADDRESS1 | NOTESONADDRESS1

Line2: REF00002 | NAME2 | ALERTADDRESS2 | LINK2DOWNLOADPDFINFOONADDRESS2 | NOTESONADDRESS2

Line3: REF00003 | NAME3 | ALERTADDRESS3 | LINK2DOWNLOADPDFINFOONADDRESS3 | NOTESONADDRESS3

etc.

I also have a line called $ jobadd, which is the address for the job ...

What do I need to do in php, if the job address is the same ($ jobadd) as any of the Alert Addresses in the txt file, then display the corresponding name, address, link and notes. It is also necessary to ignore whether it is capitalized or not. Basically, if $ jobadd = address in a txt file displays this information ...

It seems I am only echoing the last line.

+5
source share
3 answers

:

$lines = explode("\n", $data); // You may want "\r\n" or "\r" depending on the data

:

$data = array();

foreach($lines as $line) {
    $data[] = array_map('trim', explode('|', $line));
}

, $jobadd # 3, # 2, , :

foreach($data as $item) {
    if(strtolower($item[2]) === strtolower($jobadd)) {
        // Found it!
        echo "Name: {$item[1]}, link: {$item[3]}, notes: {$item[4]}";
        break;
    }
}
+4

. $file, .

$data = file_get_contents($file);

$lines = array_filter(explode("\n", str_replace("\r","\n", $data)));

foreach($lines as $line) {

    $linedata = array_map('trim', explode('|', $line));

    if(strtolower($linedata[2]) == strtolower($jobadd)) {
        // Found it!
        echo "Name: {$linedata[1]}, link: {$linedata[3]}, notes: {$linedata[4]}";
        break;
    }
}
+1
<?php

    define('JOBADDR','ALERTADDRESS3');

    # get all lines
    $pl = file_get_contents('preplan.txt');
    $pl = explode("\n",$pl); 

    # cleanup
    foreach($pl as $k=>$p){ # goes through all the lines
        if(empty($p) || strpos($p,'|')===false || strtoupper($p)!==$p /* <- this checks if it is written in capital letters, adjust according to your needs */ )
            continue;

        $e = explode('|', $p); # those are the package elements (refid, task name, address, ... )
        if(empty($e) || empty($e[2])) # $e[2] = address, it a 0-based array
            continue;

        if(JOBADDR !== trim($e[2])) # note we defined JOBADDR at the top
            continue;

        # "continue" skips the current line

        ?>


        <p>REF#<?=$e[0]; ?> </p>
        <p><b>Name: </b> <?=$e[1]; ?></p>
        <p><b>Location:</b> <a href="<?=$e[3]; ?>"><?=$e[2]; ?></a> </p>
        <p><b>Notes: </b> </p>
        <p style="text-indent:15px;"><?=empty($e[4]) ? '-' : nl2br($e[4]); ?></p>

        <hr />


        <?php
    }
0
source

All Articles