Page 2 of 2
Posted: Thu Nov 06, 2003 2:32 pm
by vigge89
how can I do if I want to store everything between 2 tags in an array then?
If the textfiles content was:
<1>
emailaddress@something.com</1>
<2>name of download</2>
<3>
http://domain.com/file.fil</3>
<4>Description of file.......
Some more description....</4>
, the array['1'] would contain the email address, array['2'] would contain the name and so on.....
Posted: Thu Nov 06, 2003 3:15 pm
by Weirdan
Code: Select all
$arr=file($filename);
foreach($arr as $key=>$val)
$arr[$key]=strip_tags($val);
Note: it will work only if line contains exactly 1 <tag></tag> pair.
Posted: Thu Nov 06, 2003 3:20 pm
by vigge89
Weirdan wrote:Code: Select all
$arr=file($filename);
foreach($arr as $key=>$val)
$arr[$key]=strip_tags($val);
Note: it will work only if line contains exactly 1 <tag></tag> pair.
well, the description will contain more than 1 line so :/
Posted: Thu Nov 06, 2003 3:25 pm
by JAM
Just play around with it abit.
Code: Select all
<?php
$text = "
<1>emailaddress@something.com</1>
<2>name of download</2>
<3>http://domain.com/file.fil</3>
<4>Description of file.......
Some more description....</4>
";
preg_match_all("/<[^>]*>([^<]*)<\/[^>]*>/",$text,$match);
echo $match[0][0].' - '.$match[0][1].' - '.$match[0][2].' - '.$match[0][3];
?>
Results:
Code: Select all
<1>emailaddress@something.com</1> - <2>name of download</2> - <3>http://domain.com/file.fil</3> - <4>Description of file.......
Some more description....</4>
Posted: Fri Nov 07, 2003 8:52 am
by vigge89
after checking the manual at PHP.net, i only get more confused over all those <[^>]*>([^<]*)<\/[^>]*>
I tested your code Jam, and my output where:
<1>
emailaddress@something.com
<2>name of download
<3>
http://domain.com/file.fil
<4>Description of file....... Some more description....
But what if the desctiption contained tags? it would then start at the last tag and continue from that one.
I'll edit the question:
The first lines with single-lined information; the email-address, name and link doesn't have to be between some tag, they're vars could maybe be set by using some sort of function to add the first three lines into different vars or an array. But then the last lines, after the first four lines (blank line between single-lined info and description), the description starts, and ends at the last line. so, I dont need the tags at all, but at first i thought that would be the easiest way.
But, how could i do this?
I know im a assh*le by just changing my mind and stuff, but im a beginner when it comes to PHP, so I have no clue on how to do this.
Posted: Sun Nov 09, 2003 2:54 am
by vigge89
noone? Come on guys, i need your help :/

Posted: Mon Nov 10, 2003 5:11 am
by JAM
Well, giving you various suggestions to how and why, you should manage.
You can combine strstr() and other functions as well, when trying to fetch the data. I'm failing to actually think of something useful as you state that 'the email-address, name and link doesn't have to be between some tag'.
I do not fully understand what you want to achieve with this anymore.
There are however alot of information about regexp's and perl's posix on the net. Try reading those for guidance. Php's internal strstr() and like-functions might be of use also.
Posted: Mon Nov 10, 2003 8:43 am
by vigge89
k, ill check the PHP manual
Posted: Mon Nov 10, 2003 9:15 am
by vigge89
hmm, i'll try using XML, but then i need a tutorial on how to start a read/write XML script. I've found one on Hotscripts.com, but that one costed 10$ if you wanted the whole, the demo part just covered reading XML files

Posted: Mon Nov 10, 2003 10:28 am
by vigge89
Checked out the tut @ zend.com (
http://www.zend.com/zend/tut/tutbarlach.php), but as all the comments said that it was full of bugs, I never finished it, so right now im looking for tutorials about XML Creation/Reading in PHP.
Anyone got a good one?
Posted: Mon Nov 10, 2003 10:34 am
by JAM
Not sure it is enough but,
http://se.php.net/manual/en/ref.xml.php
Take a look at those pages and it's usercomments first and see where that leads you.
They're indeed no tutorials, but they look very explainatory themselves...
Posted: Mon Nov 10, 2003 10:43 am
by vigge89
Code: Select all
<?php
// Extracts content from XML tag
function GetElementByName ($xml, $start, $end) {
global $pos;
$startpos = strpos($xml, $start);
if ($startpos === false) {
return false;
}
$endpos = strpos($xml, $end);
$endpos = $endpos+strlen($end);
$pos = $endpos;
$endpos = $endpos-$startpos;
$endpos = $endpos - strlen($end);
$tag = substr ($xml, $startpos, $endpos);
$tag = substr ($tag, strlen($start));
return $tag;
}
// Open and read xml file. You can replace this with your xml data.
$file = "data.xml";
$pos = 0;
$Nodes = array();
if (!($fp = fopen($file, "r"))) {
die("could not open XML input");
}
while ($getline = fread($fp, 4096)) {
$data = $data . $getline;
}
$count = 0;
$pos = 0;
// Goes throw XML file and creates an array of all <XML_TAG> tags.
while ($node = GetElementByName($data, "<XML_TAG>", "</XML_TAG>")) {
$Nodes[$count] = $node;
$count++;
$data = substr($data, $pos);
}
// Gets infomation from tag siblings.
for ($i=0; $i<$count; $i++) {
$code = GetElementByName($Nodes[$i], "<Code>", "</Code>");
$desc = GetElementByName($Nodes[$i], "<Description>", "</Description>");
$price = GetElementByName($Nodes[$i], "<BasePrice>", "</BasePrice>");
}
?>
this looks intersting, ill check it out later, also
http://phpxmlclasses.sourceforge.net/ 
Posted: Mon Nov 10, 2003 11:13 am
by vigge89
hmm, while using the code above, only thing added was echo function, the output of the $code, $desc and $price was zero (nothing).
Content of data.xml:
Code: Select all
<Code>Test</Code>
<Description>Test</Description>
<BasePrice>Test</BasePrice>
Posted: Fri Nov 14, 2003 9:55 am
by vigge89
hum, noone?
Posted: Mon Nov 17, 2003 11:26 am
by vigge89
i've made this script;
Code: Select all
<HTML>
<BODY>
<?php
//File to read
$filename = "test/test.txt";
//Open $filename
$file = fopen ($filename, "b");
//Save content of $file into $filecontent
$filecontent = fread ($file, filesize($file));
//Close file
fclose ($file);
//Separate each part of information
$content = explode ("<SEPARATOR>", $filecontent);
//Set content variables
$dname = $content[0];
$dauthor = $content[1];
$demail = $content[2];
$uf_dsite = $content[3];
$ddesc = $content[4];
//Transform linebreaks into <br> for description
nl2br($ddesc);
//Check if $uf_dsite is empty, if it is, set site to "None". Else create a link for it.
If (empty($dsite)) {
$dsite = "None";
elseif ($dsite == "http://") {
$dsite = "None";
else {
$dsite = "<a href="$uf_dsite" target="_blank">$uf_dsite</a>";
}
echo "
Name: $dname<br>
Author: $dauthor<br>
Email: $demail<br>
Website: $dsite<br>
Description:<br>
$ddesc
?>
</BODY>
</HTML>
and it works well, but are there any better methods like this?