Results 1 to 6 of 6

Thread: How to search a word in text file.

  1. #1
    Join Date
    Oct 2008
    Posts
    40
    Thanks
    3
    Thanked 1 Time in 1 Post

    Smile How to search a word in text file.

    Hello ,

    I want to search a word from a text file.
    e.g search word:- hello, hi welcome to php
    Search is separated by comma or by space.

    Consider there is one directory that contain many files and i want to search that word in every file. if "hello hi welcome to php" words found in these file not continuously .The result will show the link to these file. when user will click on these file it will show the contain (Search word will be shown in different color like in yellow color).

    File may contain these word (HELLO HI WELCOME to Php) in capital but still i want to show the result .

    e.g
    HTML Code:
    <input type=text name=txtSearch id=idSearch><input type=button value=SEARCH onclick='search(idSearch.value);'>
    Result will show the links to these file ..

    File1.log
    File2.log
    File3.log
    File4.log

    When user will click on these link then file contain will be show to user . These search word will be shown in different color (like in yellow color).

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    1,720
    Thanks
    82
    Thanked 90 Times in 88 Posts

    Default

    I know you said this already, but just to confirm, you want to search .txt files?
    To choose the lesser of two evils is still to choose evil. My personal site

  3. #3
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    1,720
    Thanks
    82
    Thanked 90 Times in 88 Posts

    Default

    well.... here is a little something to get you started if this is the route you really want to go.

    PHP Code:
    <?php
    $handle 
    file_get_contents("test_text.txt",NULL);
    $a="hello, hi welcome to php";
    $a=str_replace(",","",$a);
    $a=explode(" ",$a);
    $c=0;
    foreach(
    $a as $y){
    if (
    stristr($handle,"$a[$c]")) $b[]= 'yes';
    else 
    $b[]='no';
    $c++;
    }
    echo 
    $handle;
    if (
    in_array("no",$b)) echo '<br><br>did not match';
    else echo 
    '<br><br>we have a match';
    ?>
    It will scan one document for the selected terms. If they all appear then "we have a match". If one of the terms is not found in the document then "did not match".

    You should be able to figure the rest out from here with the possible exception of gathering all of the file names in a directory. Either way this should help you get started. Let us know if you get stuck anywhere.

    EDIT: I kinda regret posting the above code. It does have its uses for correcting poor coding across directories of bad files, but to use it as a search program is really a poor way to use it. data like this should be stored in a database like MySQL and searched that way. You will have far greater control over your searches and the coding will be much simpler as well.
    Last edited by james438; 11-18-2008 at 05:00 PM.
    To choose the lesser of two evils is still to choose evil. My personal site

  4. #4
    Join Date
    Feb 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by james438 View Post
    well.... here is a little something to get you started if this is the route you really want to go.

    PHP Code:
    <?php
    $handle 
    file_get_contents("test_text.txt",NULL);
    $a="hello, hi welcome to php";
    $a=str_replace(",","",$a);
    $a=explode(" ",$a);
    $c=0;
    foreach(
    $a as $y){
    if (
    stristr($handle,"$a[$c]")) $b[]= 'yes';
    else 
    $b[]='no';
    $c++;
    }
    echo 
    $handle;
    if (
    in_array("no",$b)) echo '<br><br>did not match';
    else echo 
    '<br><br>we have a match';
    ?>
    It will scan one document for the selected terms. If they all appear then "we have a match". If one of the terms is not found in the document then "did not match".

    You should be able to figure the rest out from here with the possible exception of gathering all of the file names in a directory. Either way this should help you get started. Let us know if you get stuck anywhere.

    EDIT: I kinda regret posting the above code. It does have its uses for correcting poor coding across directories of bad files, but to use it as a search program is really a poor way to use it. data like this should be stored in a database like MySQL and searched that way. You will have far greater control over your searches and the coding will be much simpler as well.
    hie ,
    a little question, why this script doesn't work?
    <?php
    $f='test.txt';
    $t='kaveh';
    $tp=fopen($f,'r');
    $fd=file($f);
    for($i=0;$i<=count($fd);$i++){
    $temp=$fd[$i];
    if($temp==$t){
    echo 'found on:'.$i;
    }
    }
    fclose($tp);
    ?>

  5. #5
    Join Date
    Mar 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    i have urdu dictionary file txt and csv and xls in unicode i want search any word in dictionary with php?

    example word : ابد
    or
    عابد
    or
    مستقبل

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    If this is a new question, start a new thread.
    If this is related to the topic here, explain why, and what the problem is.

    Generally speaking, unicode will not be a problem in PHP as long as you have the right character encoding set as every step of the process-- in the HTML, in the encoding of the PHP document itself (the text of the file itself must be saved that way-- you can check in Notepad), in your other text file, and potentially other places like a database.

    It'll probably be better to do this in a database if you have a long list, but it's up to you.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

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