Results 1 to 6 of 6

Thread: Retrieving text between ...

  1. #1
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Retrieving text between ...

    Hello everyone,

    I am new to PHP so please excuse me for any mistakes I make in this part of the forum. What I wanted to know is that can't I retrieve the text between special characters from a seperate file?? For e.g. I have a file called test.txt which has the following contents:

    Code:
    === Title ===
    
    This is a test page with a title in bold.
    
    === Title2 ===
    
    This is another test with second title.
    Now I want to retrieve that file with PHP and get ONLY the text between the starting === & === the ending. Any ideas how I can achieve that??

    I am using dokuwiki and want to get the section title by the way.

    Any helps would be greatly appreciated.

  2. #2
    Join Date
    Jun 2006
    Posts
    148
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Why don't you just put them in separate text files? I'm a Bit confused at what your trying to say. But try that.
    The code to insert the text in a txt file is
    Code:
    file_get_contents('YourFile.txt')
    Happy To Help,
    Joe
    Cheap Webhosting At ONE TIME fees! Also offering Scripts, Software, Web Designs, and Graphic Designs. Outstanding Designs and Webhosting!

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    function getHeaders($content, $delimiter = '===') {
      $tc = $content;
      $titles = array();
      while(strpos($content, $delimiter) !== false) {
        $cttl = $tc;
        $cttl = substr($cttl, strpos($cttl, $delimiter) + strlen($delimiter));
        $cttl = substr($cttl, 0, strpos($cttl, $delimiter));
        $cttl = trim($cttl);
        array_push($titles, $cttl);
        $tc = substr($tc, strlen($cttl) + strlen($delimiter));
      }
      return $titles;
    }
    
    function getSection($content, $section) {
      $tc = $content;
      $tc = substr($tc, strpos($tc, $section . ' ===') + 4);
      $tc = substr($tc, 0, strpos($tc, '==='));
      return $tc;
    }
    
    $fc = file_get_contents("file.txt");
    $ttls = getHeaders($fc);
    I wrote getSection() before I read your post properly, but figured you'd probably want it later on so I left it in there anyway.

    Untested.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks Twey, let me try that and thanks joe for you response, I will try to make it clearer later on after I test this code Twey posted.

  5. #5
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Twey I am continuously getting an error which says:


    Fatal error: Allowed memory size of 12582912 bytes exhausted (tried to allocate 35 bytes) in /var/www/html/shachi/testget.php on line 10

    Where the blue part keeps on changing(as the content in the file file.txt keeps on increasing).

    Thanks.

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Ah, that's because the file is too big to read into memory. You'll have to devise some method of splitting it into chunks.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •