Page 1 of 2

Content grabbing from txtfile

Posted: Thu Nov 06, 2003 10:05 am
by vigge89
Im creating a download management system for my site, and im using text-files to store the information of the downloads, like description, dl-link, name and author. Now i want everything except the description to be saved as one line only. The description would be in some sort of tag, like
<DESC>Description here....</DESC>
This is where i need help, i want to know how to load everything between the tags into a variable, that later will be printed on a page. How can I do that?

Posted: Thu Nov 06, 2003 10:18 am
by maniac9
You might want to try doing it as some sort of XML. Maybe keep the DESC tags, and make tags for everything else, and then just parse that information XML-style from the file, or even transform it with XSL.

Posted: Thu Nov 06, 2003 10:18 am
by Weirdan
It depend on how do you load file which, in turn, depends on how big it is. So how do you load that file? ;)

Posted: Thu Nov 06, 2003 10:19 am
by vigge89
maniac9 wrote:You might want to try doing it as some sort of XML. Maybe keep the DESC tags, and make tags for everything else, and then just parse that information XML-style from the file, or even transform it with XSL.
can PHP grab the xml-file and print it?

Posted: Thu Nov 06, 2003 10:20 am
by JAM
Regular expressions

Code: Select all

<?php
$string = 'example <desc>FOO in here</desc> example example example...';
if (ereg ("<+(.*)+>", $string, $regs)) {
    echo $regs[0];
}

// <desc>FOO in here</desc>
?>
Edit: Just as an example for more ideas.

Posted: Thu Nov 06, 2003 10:22 am
by Weirdan
vigge89 wrote:can PHP grab the xml-file and print it?
Sure thing. More information available here

Posted: Thu Nov 06, 2003 10:24 am
by vigge89
ill test JAMs one out first, since ive already made a download creater for txt-files

Posted: Thu Nov 06, 2003 10:28 am
by Weirdan
JAM wrote:Regular expressions

Code: Select all

<?php
$string = 'example <desc>FOO in here</desc> example example example...';
if (ereg ("<+(.*)+>", $string, $regs)) {
    echo $regs[0];
}

// <desc>FOO in here</desc>
?>
Edit: Just as an example for more ideas.
NOTE: since ereg* functions are greedy, you can't parse in this way chunk of text, containing more then one <desc></desc> pair. Look at preg_* family of functions or use this POSIX regexp:
"<([^>]*)>([^<]*)<(/[^>]*)>"

then use $regs[1] to get
// FOO in here

Posted: Thu Nov 06, 2003 10:28 am
by vigge89
JAM wrote:Regular expressions

Code: Select all

<?php
$string = 'example <desc>FOO in here</desc> example example example...';
if (ereg ("<+(.*)+>", $string, $regs)) {
    echo $regs[0];
}

// <desc>FOO in here</desc>
?>
Edit: Just as an example for more ideas.

Code: Select all

<?php

$string = 'example <desc>FOO in here</desc> example example example <2>2nd</2>';
if (ereg ("<+(.*)+>", $string, $regs)) {
    echo $regs[0];
	echo $regs[1];
}

?>
outputs:

FOO in here example example example <2>2nd

Posted: Thu Nov 06, 2003 10:30 am
by vigge89
so, how should i do?
if i use XML, which ive never used before, but for what i've seen of it, it looks much easier, but then, how should i write a download submitter/adder / editor?

Posted: Thu Nov 06, 2003 10:38 am
by Weirdan
vigge89 wrote:so, how should i do?
if i use XML, which ive never used before, but for what i've seen of it, it looks much easier, but then, how should i write a download submitter/adder / editor?
Look at my suggestion ;)

Posted: Thu Nov 06, 2003 10:40 am
by vigge89
Weirdan wrote: Look at my suggestion ;)
that ouputs;
FOO in heredesc
, and
<desc>FOO in here</desc>desc
as source when executing

Code: Select all

<?php

$string = "example <desc>FOO in here</desc> example example <2>2nd</2>";
if (ereg ("<([^>]*)>([^<]*)<(/[^>]*)>" , $string, $regs)) {
    echo $regs[0];
	echo $regs[1];
}

?>

Posted: Thu Nov 06, 2003 10:45 am
by Weirdan
Yeah, I see I was wrong.
try

Code: Select all

preg_match("/<([^>]*)>([^<]*)<(\/[^>]*)>/",$text,$matches);
echo $match[2];

Posted: Thu Nov 06, 2003 10:57 am
by vigge89
Weirdan wrote:Yeah, I see I was wrong.
try

Code: Select all

preg_match("/<([^>]*)>([^<]*)<(\/[^>]*)>/",$text,$matches);
echo $match[2];
PHP Code:

Code: Select all

<?php
$text = "example <desc>FOO in here</desc> example example <2>2nd</2>";

preg_match("/<([^>]*)>([^<]*)<(\/[^>]*)>/",$text,$match);
echo $match[0]; 
echo "<br>"; 
echo $match[1]; 
echo "<br>"; 
echo $match[2]; 
echo "<br>"; 
echo $match[3];
?>
And output:
FOO in here
desc
FOO in here
/desc

Posted: Thu Nov 06, 2003 11:12 am
by JAM
Weirdan wrote: NOTE: since ereg* functions are greedy, you can't parse in this way chunk of text, containing more then one <desc></desc> pair. Look at preg_* family of functions or use this POSIX regexp:
"<([^>]*)>([^<]*)<(/[^>]*)>"

then use $regs[1] to get
// FOO in here
Yup, hence my note in the same post I edited in.
I was just making a point as I thought parsing it as xml (or whatever) seemed overkill for accomplish a solution...

As the original question was to get the contents,

Code: Select all

<?php
$text = "example <desc>FOO in here</desc> example example <2>2nd</2>";
preg_match("/<[^>]*>([^<]*)<\/[^>]*>/",$text,$match);
echo $match[1];
?>
...would be sufficient.