SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot amit290's Avatar
    Join Date
    Oct 2001
    Posts
    158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    get txt between points and store in mysql

    How could I read a HTML document and get text between 2 points from it??

    start point looks like this = <h2>Want to know more</h2>
    end point looks like this = <h2>What is this course about</h2>

    I want the text (html) extracted and put into a database.

    Hope someone can help,

    Thanks

  2. #2
    SitePoint Enthusiast
    Join Date
    Mar 2004
    Location
    Ukraine, Khmelnitsky
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    file_get_contents
    preg_match
    mysql_query
    Sergey Bres aka tserbis, Ukraine

  3. #3
    SitePoint Guru
    Join Date
    Jul 2004
    Location
    Raleigh, NC
    Posts
    783
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use what tserbis said but replace his middle step with this:

    PHP Code:
    $pattern "|$start(.*?)$end|s";
    preg_match_all($pattern$text$matches); 

  4. #4
    SitePoint Zealot amit290's Avatar
    Join Date
    Oct 2001
    Posts
    158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thnx for the replies, but I cant figure the preg_match bit out . Can anyone do me the code so it does what I asked in the original post? please?

    thnx

  5. #5
    SitePoint Addict loadx's Avatar
    Join Date
    Jul 2004
    Location
    Melbourne, VIC, Australia
    Posts
    221
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $text '<h2>Sample</h2>';

    $start '<h2>';
    $end '</h2>';

    $pattern "|$start(.*?)$end|s";
    $foo =  preg_match_all($pattern$text$matches); 
    print_r($matches); // this is the matches array 
    From the manual:

    preg_match_all -- Perform a global regular expression match
    Description
    int preg_match_all ( string pattern, string subject, array matches [, int flags [, int offset]])


    Searches subject for all matches to the regular expression given in pattern and puts them in matches in the order specified by flags.

    After the first match is found, the subsequent searches are continued on from end of the last match.

    flags can be a combination of the following flags (note that it doesn't make sense to use PREG_PATTERN_ORDER together with PREG_SET_ORDER):

    link: http://www.php.net/preg_match_all/

  6. #6
    SitePoint Zealot amit290's Avatar
    Join Date
    Oct 2001
    Posts
    158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thnx , getting somewhere now! . When i execute the above I get output like this....

    PHP Code:
    Array ( [0] => Array ( [0] => 
    Course Facts
     
    UCAS course codes
    D400 BSc/WAgD401 BSc/WA (with placement year)
     
    Length3 years (4 years with placement
     
    Entry Requirements
     
    ) [1] => Array ( [0] => 
    UCAS course codesD400 BSc/WAgD401 BSc/WA (with placement year)
     
    Length3 years (4 years with placement
     
    ) ) 
    I need to just print the array of text between the start and end points.... so in this case, I would want to echo:

    UCAS course codes: D400 BSc/WAg; D401 BSc/WA (with placement year)
    Length: 3 years (4 years with placement)

    Im not sure which array this is, or how to go about finding it. This array will need to be a string so I can insert it into database. Its there somewhere, but how do I access it? Seems like theres an array within an array.


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
  •