Forum Moderators: coopster
I am using nusoap to retrieve data from an web service. I want to simple display the result in an html table that is at most 15 items 3x5cells.
The xml is failry straight forward
<person>
<Name>Name 1</Name>
<age>32</age>
<phone>blah blah</phone>
<etc>blah blah</etc>
</person>
<person>
<Name>Name 2</Name>
<age>24</age>
<phone>blah blah</phone>
<etc>blah blah</etc>
</person>
I looked the archives here and for some scripts at hotscripts and I didn't see anything very simple. I have PHP4 so I can't take advantage of Simple XML extension or easily use xslt.
Can anyone point me in a diredtion/tutorial to get this type of task easily done? I'm a hack when it comes to php.
Thanks,
$text="<person>
<Name>Name 1</Name>
<age>32</age>
<phone>blah blah</phone>
<etc>blah blah</etc>
</person>";
$a=0;
while($row=parse_tag("person",$text))
{
$a++;
$data[$a][Name]=parse_tag("Name",$row);
$data[$a][age]=parse_tag("age",$row);
//... etc ...
$text=substr($text,strpos($text,$row),strlen($row));
}
print "<table>";
for ($a=0;$a<sizeof($data);$a++)
{
print "<tr><td>".$data[$a][Name]."</td>";
print "<td>".$data[$a][age]."</td>";
print "<td>".$data[$a][phone]."</td></tr>";
//... etc ...
}
print "</table>";
function parse_tag($tag,$text)
{
//make it preg-safe
$tag=preg_replace("/[^0-9a-z]/i","\$1",$tag);
//find the text
preg_match("/\<$tag\>(.*?)\<$tag\>/ism",$text,$match);
if ($match[1]) return $match[1];
else return false;
}
Oops, badly copied. I missed the brackets. Basically it adds a \ before thigns that aren't numbers or letters (as these are reserved as special regex functions). The preg_match should then match the text within the tag.
//make it preg-safe
$tag=preg_replace("/([^0-9a-z])/i","\$1",$tag);
FYI, I just learned in another thread that preg_quote() [php.net] does exactly the same...