Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Feb 2006
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Search in a file (advanced)

    Hello everybody!
    First of all, I should thank you in advance for reading this thread.
    Please, help me if you can because it is urgent.


    1. What do I want to do?
    I wish to create a search <FORM> to search in the contents of a text file located somewhere on the server. I wish to be able to:
    a) set a search criteria which is different from exact phrase
    Example: Searching Blue monkey happy should find Happy blue monkey

    b) set a search criteria with case INsensitive
    Example: Searching HAPPY BlUe MonKeY should find Happy blue monkey

    c) as the text file contains lines with a unique record on each; and on each line there are words separated by a TAB delimiter, I would like:

    - if a part of the line in the file matches the search criteria, the WHOLE line has to be printed;

    - results have to be printed in a <TABLE> with <TR> for every line match and <TD> for every word between the TAB delimiters in the file.

    2. What have I done so far?
    I got this code. It shows the contents of the text file in a table but all of them - not just the ones matching the search criteria...
    <?php
    function convert_to_table($line) {
    return '<tr><td>'.str_replace("\t", '</td><td>', $line).'</td></tr>';
    }

    function tabfile_to_rows($file) {
    return implode('', array_map('convert_to_table', file($file)));
    }

    echo '<table>'.tabfile_to_rows('test.txt').'</table>';

    ?>
    3. How should it look like?
    If the text file looks like this:
    John Smith TAB The Blue Ocean TAB 992 TAB 91sd-155523-d TAB 11,84
    Beverly Smith TAB Carrots are Bizarre TAB 211 TAB 555c-1js8ue-1 TAB 30,90
    Carl Bonnen TAB Ridiculous CarrotsTAB 100 TAB aaaa-ya7ay8-1 TAB 28,99
    Niki Storm TAB Bizarre Carrots Today TAB 1855 TAB 180a-000h6s-x TAB 199,99
    Palm Dox TAB Minimods TAB 33 TAB x222-2121dd-0 TAB 6,00
    and if I search for

    CARROTS Bizarre
    I should get
    <TR><TD>Beverly Smith </TD><TD> Carrots are Bizarre </TD><TD> 211 </TD><TD> 555c-1js8ue-1 </TD><TD> 30,90</TD></TR>
    <TR><TD>Carl Bonnen </TD><TD> Ridiculous Carrots</TD><TD> 100 </TD><TD> aaaa-ya7ay8-1 </TD><TD> 28,99 </TD></TR>
    <TR><TD>Niki Storm </TD><TD> Bizarre Carrots Today </TD><TD> 1855 </TD><TD> 180a-000h6s-x </TD><TD> 199,99 </TD></TR>
    Please, if you believe you can handle that - help me!!!

  • #2
    Regular Coder
    Join Date
    Mar 2005
    Location
    Brighton, UK
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Edit: Sorry, i got confused with your other topic that covered the same thing (and the pm you sent me), this code will convert the tabbed data to an html table only.

    Hi Shadowfox, here you go:

    PHP Code:
    <?php 

    $mydata 
    '
    Name 1        e-mail 1        Age 1
    Name 2        e-mail 2        Age 2
    Name 3        e-mail 3        Age 3
    Name 4        e-mail 4        Age 4
    Name 5        e-mail 5        Age 5
    '
    ;

    $mydata trim($mydata);

    echo 
    '<table border="1">';
    $rows preg_split('/\n/'$mydata0);
    foreach(
    $rows as $row) {
        echo 
    '<tr>';
        
    $cells preg_split('/\t+/'$row0);
        foreach(
    $cells as $cell) {
            echo 
    '<td>'.$cell.'</td>';
        }
        echo 
    '</tr>';
    }
    echo 
    '</table>';

    ?>
    Enjoy!

    Jack

    P.S. If you copy and paste this example it wont work because the tabs in $mydata are replaced with spaces on the forum, use your own files content instead (use file_get_contents).
    Last edited by Jak-S; 02-15-2006 at 08:08 PM.

  • #3
    Regular Coder
    Join Date
    Mar 2005
    Location
    Brighton, UK
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts
    And this does the search as well:

    PHP Code:

    <?php 

    $mydata 
    '
    Name1        e-mail1        Age1
    Name2        e-mail2        Age2
    Name1        e-mail3        Age3
    Name4        e-mail4        Age4
    Name5        e-mail5        Age5
    '
    ;

    $mydata trim($mydata);

    $mycriteria 'name1 AGE';

    echo 
    '<table border="1">';

    $rows preg_split('/\n/'$mydata, -1);
    foreach(
    $rows as $row) {
        
        
    $found 0;
        
    $keywords preg_split('/\s/'$mycriteria, -1);
        foreach(
    $keywords as $keyword) {
            if(
    preg_match('/.*'.$keyword.'.*/is'$row)) $found++;
        }
        
        if(
    $found == count($keywords)) {
            echo 
    '<tr>';
            
    $cells preg_split('/\t+/'$row, -1);
            foreach(
    $cells as $cell) {
                echo 
    '<td>'.$cell.'</td>';
            }
            echo 
    '</tr>';
        }
        
    }

    echo 
    '</table>';

    ?>

  • #4
    New Coder
    Join Date
    Feb 2006
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you! Thank you! Thank you!
    What would you recommend me to change in order to:

    place a checkbox in front of each row containing name="id number (autonumber)" value="the value of the whole row"

    The code below works.. but still.. do you approve the syntax and the way it's done?

    PHP Code:
    <?php 

    $mydata 
    '
    Name1    e-mail1    Age1
    Name2    e-mail2    Age2
    Name1    e-mail3    Age3
    Name4    e-mail4    Age4
    Name5    e-mail5    Age5
    '
    ;

    $mydata trim($mydata);

    $mycriteria 'na';

    echo 
    '<form name="sf" method="POST"><table border="1">';

    $rows preg_split('/\n/'$mydata, -1);
    $an 1;
    foreach(
    $rows as $row) {
        
        
    $found 0;

        
    $keywords preg_split('/\s/'$mycriteria, -1);
        foreach(
    $keywords as $keyword) {
            if(
    preg_match('/.*'.$keyword.'.*/is'$row)) $found++;
        }
       
        if(
    $found == count($keywords)) {
            echo 
    '<tr><td><input type="checkbox" name="' $an '" value="' $row '" /></td>';
            
    $cells preg_split('/\t+/'$row, -1);
            foreach(
    $cells as $cell) {
                echo 
    '<td>'.$cell.'</td>';
            }
            echo 
    '</tr>';
        }
       
    $an++;
    }

    echo 
    '</table><input type="submit" value="Bring it on" /></form>';

    ?>
    Is that ok?
    Last edited by Shadowfox; 02-15-2006 at 09:12 PM.

  • #5
    Regular Coder
    Join Date
    Mar 2005
    Location
    Brighton, UK
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah it looks ok, i mean, almost all code can be improved on to make it more flexible etc. but this does everything you need and is the simplest solution really. I would stick with it, if that does everything you need it too there is no point over complicating things.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •