Content grabbing from txtfile
Moderator: General Moderators
Content grabbing from txtfile
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?
<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?
Regular expressions
Edit: Just as an example for more ideas.
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>
?>
Last edited by JAM on Thu Nov 06, 2003 10:22 am, edited 1 time in total.
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:JAM wrote:Regular expressionsEdit: Just as an example for more ideas.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> ?>
"<([^>]*)>([^<]*)<(/[^>]*)>"
then use $regs[1] to get
// FOO in here
JAM wrote:Regular expressionsEdit: Just as an example for more ideas.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> ?>
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];
}
?>FOO in here example example example <2>2nd
that ouputs;Weirdan wrote: Look at my suggestion
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];
}
?>Yeah, I see I was wrong.
try
try
Code: Select all
preg_match("/<([^>]*)>([^<]*)<(\/[^>]*)>/",$text,$matches);
echo $match[2];PHP Code:Weirdan wrote:Yeah, I see I was wrong.
tryCode: Select all
preg_match("/<([^>]*)>([^<]*)<(\/[^>]*)>/",$text,$matches); echo $match[2];
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];
?>FOO in here
desc
FOO in here
/desc
Yup, hence my note in the same post I edited in.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
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];
?>