Forum Moderators: coopster

Message Too Old, No Replies

Displaying XML with PHP 4

         

MrSpeed

3:23 pm on Jul 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I realize this is another question that has a simple answer.

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,

vincevincevince

9:41 am on Jul 31, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




$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;
}

MrSpeed

1:41 pm on Aug 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What do these lines do? Replace all characters that are not numbers or letters? I don't understane the $1 part.

//make it preg-safe
$tag=preg_replace("/[^0-9a-z]/i","\$1",$tag);
//find the text
preg_match("/\<$tag\>(.*?)\<$tag\>/ism",$text,$match);

vincevincevince

4:04 pm on Aug 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



//make it preg-safe
$tag=preg_replace("/([^0-9a-z])/i","\$1",$tag);
//find the text
preg_match("/\<$tag\>(.*?)\<$tag\>/ism",$text,$match);

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.

RonPK

5:58 pm on Aug 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



//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...

vincevincevince

8:37 pm on Aug 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's a very useful thing that preg_quote(), I shall make great use of it, thanks