Log in

View Full Version : Retrieving text between ...



shachi
06-28-2006, 06:31 PM
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:



=== 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.

jad9321
06-28-2006, 06:43 PM
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

file_get_contents('YourFile.txt')

Happy To Help,
Joe

Twey
06-28-2006, 06:59 PM
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.

shachi
06-29-2006, 09:28 AM
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.:)

shachi
06-29-2006, 09:35 AM
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.

Twey
06-29-2006, 03:02 PM
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.