Content grabbing from txtfile

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Content grabbing from txtfile

Post 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?
maniac9
Forum Commoner
Posts: 55
Joined: Fri Aug 01, 2003 12:27 am
Location: Arkansas, USA
Contact:

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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? ;)
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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.
Last edited by JAM on Thu Nov 06, 2003 10:22 am, edited 1 time in total.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

vigge89 wrote:can PHP grab the xml-file and print it?
Sure thing. More information available here
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

ill test JAMs one out first, since ive already made a download creater for txt-files
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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 ;)
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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];
}

?>
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Yeah, I see I was wrong.
try

Code: Select all

preg_match("/<([^>]*)>([^<]*)<(\/[^>]*)>/",$text,$matches);
echo $match[2];
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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.
Post Reply