SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    extracting specific text from a text using php

    Hello guys, I am new to php and running a project. I want to extract some text i specify from a text file as in the example below:

    $GPRMC,040135.000,V,,,,,,,190810,,,N*4F

    I want to extract the above lines of text from among other separate text which are in the text file. The above line is just how it appears in the note pad. I think with php i can be able to do it but is hard to figure out.

    I need a clue on how to go about it. Thanks in advance

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    In order for someone to help you it is best to post a sample of the problem, which in you case means 1 or more lines which you do not want to catch, along with a 1 or more lines you do want to filter out.

    Then tell us the rule which has to be applied, or show exactly the result you want.

    Create an explicit "before" and "after".

  3. #3
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Assuming each line of text contains fields of data separated by commas, you can read each line of text into a text string and then use explode() to break up the line into the various data fields and then use just the data fields you need to.

  4. #4
    SitePoint Enthusiast Phyxas's Avatar
    Join Date
    Mar 2010
    Location
    Houston, TX
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, so you want to be able to read a file from your server, directory, on your website, without linking it?

    Try this:

    PHP Code:

    <?php

    /// This is the fopen()-function, which allows you to open certain files or urls. Also, "r" stands for read the file only. 

    $file fopen("notepad.txt""r");

    // Does the file even exist? If not, we give an error message.

    if (file_exists($file)) {
        echo 
    "The file $file exists and can is readable.";

    elseif {
        echo 
    "The file $file does not exist, and is not be readable.";
    }
    else {
        echo 
    $file;
    }

    ?>

  5. #5
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    In order for someone to help you it is best to post a sample of the problem, which in you case means 1 or more lines which you do not want to catch, along with a 1 or more lines you do want to filter out.

    Then tell us the rule which has to be applied, or show exactly the result you want.

    Create an explicit "before" and "after".
    Thanks for your reply to my thread. To answer your question. Below is a sample of the raw data i have in a text file.

    07:00:57
    IMEI 358998018395510
    $GPRMC,040100.284,V,,,,,,,190810,,,N*47
    CurCell 273953 LAC 29120 Name elisa MCC 244 MNC 05 MODE 6 SSI 86
    07:01:09
    IMEI 358998018395510
    $GPRMC,040111.000,V,,,,,,,190810,,,N*49
    $GPGGA,040108.000,,,,,0,,,,,,,,*75
    CurCell 273953 LAC 29120 Name elis
    a MCC 244 MNC 05 MODE 6 SSI 86

    I want to extract the highlited entries above .ie the ones starting with $ and all the parameters that follow. The $GPRMC must be separated from $GPGA. This is want i want and the output would look like this:

    $GPRMC,040142,A,6009.3358,N,02444.6373,E,1.62,325,190810,,
    $GPRMC,040154,A,6009.3333,N,02444.6380,E,0.00,0,190810,,

    I hope my explanation is understandable. Many thanks

  6. #6
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your respons. I am reading a file from a text file which is in my www directory. The text file contains raw data and I want to extract the data I want. The raw data looks like this:
    07:00:57
    IMEI 358998018395510
    $GPRMC,040100.284,V,,,,,,,190810,,,N*47
    CurCell 273953 LAC 29120 Name elisa MCC 244 MNC 05 MODE 6 SSI 86
    07:01:09
    IMEI 358998018395510
    $GPRMC,040111.000,V,,,,,,,190810,,,N*49
    $GPGGA,040108.000,,,,,0,,,,,,,,*75
    CurCell 273953 LAC 29120 Name elisa MCC 244 MNC 05 MODE 6 SSI 86

    And I want to separate the $GGPRMC and $GPGGA and all the the other parameters that follow on the same line. The extracted data would like this:

    $GPRMC,040142,A,6009.3358,N,02444.6373,E,1.62,325,190810,,
    $GPRMC,040154,A,6009.3333,N,02444.6380,E,0.00,0,190810,,

    Hope this explanation makes sense. Many thanks.

    Best Regards

  7. #7
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,087
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Question also asked in another thread: http://www.sitepoint.com/forums/showthread.php?t=701994

    Thread closed.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy


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
  •