Menu
Hi all,
Alas, I found out my host is only running php4. I read that php5 has new simplexml functions that make life easier.
I wish to read xml or txt files in and convert them (styled , tabulated) into html.
Is there an easy way to do this? I assume using xml is better than trying to read in text.
I do not want to simply cut and paste the whole file but rather look for certain tags to place and style them differently. As such I assume XML is a better choice?
Thanks.
[CODE]function get_xmlvalue($val, $contents) {
if (ereg($val, $contents)==true) {
$chars = strlen($val);
$valbegin = "<".$val.">";
$valend = "</".$val.">";
$beginpos = strpos($contents, $valbegin);
$endpos = strpos($contents, $valend);
$dif = ($endpos - $beginpos) - ($chars+2);
return substr($contents, ($endpos-$dif), $dif);
}
}
function get_xmlvalue_multi($val, $contents) {
$split_data = explode("<".$val, $contents);
$num = count($split_data);
for($i=1; $i<$num; $i++) {
$data[$i] = get_xmlvalue($val, "<".$val.$split_data[$i]);
}
return $data;
}[/CODE]
[CODE]$items = get_xmlvalue_multi("item", file_get_contents(XML));
for($i=0; $i<count($items); $i++) {
echo 'Name of item '.$i.' = '.get_xmlvalue("name", $item[$i]).'<br />';
echo 'Description of item '.$i.' = '.get_xmlvalue("description", $item[$i]).'<br /><br />';
}[/CODE]
[CODE]This is the name
this is the description
This is the name2
this is the description2[/CODE]
[CODE]$items = get_xmlvalue_multi("item", file_get_contents(XML));
for($i=0; $i<count($items); $i++) {
echo 'Name of item '.$i.' = '.get_xmlvalue("name", $item[$i]).'<br />';
echo 'Description of item '.$i.' = '.get_xmlvalue("description", $item[$i]).'<br /><br />';
}[/CODE]
[CODE]
<e1>
<e2>
<e3></e3>
<e4 attr1="xx" attr2="yy" attr3="zz">Some Name</e4>
<e4 attr1="aa" attr2="bb" attr3="cc">Some Name2</e4>
<e4 attr1="ii" attr2="jj" attr3="kk">Some Name3</e4>
</e2>
</e1>
[/CODE]
[CODE]function find($val, $contents) {
$parts1 = explode($val.'="', $contents);
$parts2 = explode('"', $parts1[1]);
return $parts2[0];
}[/CODE]
[CODE]echo find('att2', $contents);[/CODE]
[CODE]$items = get_xmlvalue_multi("ext ", file_get_contents('/home/admin/extensions_list.xml'));
for($i=0; $i<count($items); $i++) {
//echo 'Name of item '.$i.' = '.get_xmlvalue("ext", $items[$i]).'<br />';
//echo 'Description of item '.$i.' = '.get_xmlvalue("description", $items[$i]).'<br /><br />';
echo '<b>'.find("creator", $items[$i]).'</b><br/>';
echo find("description", $items[$i]).'<br/>';
echo '<a href="'.find("homepageURL", $items[$i]).'">'.find("homepageURL", $items[$i]).'</a><br/><br/>';
}[/CODE]
Have you changed something since posting this, I just had a look at the output page you linked to and it looks alright. The xml you linked to also does have items on there that aren't on the output page but I noticed the output page has items that aren't on the xml page, are you taking info from multiple pages?
I put the code into a page myself:[URL]http://www.k07.net/testt.php[/URL] and got all the entries coming up bar the last one (so I changed it to $i<count($items)+1; )[/QUOTE]
[CODE]<ext version="0.3" disabled="false" homepageURL="http://philringnalda.com/mozilla/blogthis/" description="Adds right-click access to Blogger's BlogThis popup." creator="Phil Ringnalda" updateURL="http://philringnalda.com/mozilla/blogthis/update.rdf" id="{8F82D6F9-D8F0-4477-8C73-908531D73538}">BlogThis</ext>[/CODE]
[CODE]function find($val, $contents) {
$parts1 = explode($val.'="', $contents);
$parts2 = explode('"', $parts1[1]);
return $parts2[0];
}
function title($contents) {
$temp = explode(">", $contents);
return substr($temp[1], 0, -1);
}
function get_xmlvalue_multi($val, $contents) {
$split_data = explode("<".$val, $contents);
return $split_data;
}
$items = get_xmlvalue_multi("ext ", file_get_contents('http://www.stevelim.com/extensions_list.xml'));
for($i=0; $i<count($items)+1; $i++) {
echo '<b>'.title($items[$i]).'</b><br/>';
echo '<b>'.find("creator", $items[$i]).'</b><br/>';
echo find("description", $items[$i]).'<br/>';
echo find("version", $items[$i]).'<br/>';
echo '<a href="'.find("homepageURL", $items[$i]).'">'.find("homepageURL", $items[$i]).'</a><br/><br/>';
}[/CODE]
[CODE]function title($contents) {
$temp = explode(">", $contents);
return substr($temp[1], 0, -5);
} [/CODE]
[CODE]
for($i=1; $i<=count($items); $i++) {[/CODE]
0.1.9 — BETA 6.2