View Full Version : Help with pregmatch

11-05-2007, 06:06 PM
So Im trying to parse an XML feed and everything seems to be in order. Yet it will only parse the first variable '$online' correctly. The second variable returns nothing. Any idea why, anyone? :confused:

$h3tag = $_GET['h3tag'];
if (empty($h3tag)) {
$h3tag = 'blank';

$h3tag = str_replace(" ","%20",$h3tag);

$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, 'http://duncanmackenzie.net/services/GetXboxInfo.aspx?GamerTag='. $h3tag);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$in1 = curl_exec($ch);

preg_match('#<Info>(.+?)</Info>#',$in1, $online1);
preg_match('#<Info2>(.+?)</Info2>#',$in1, $status1);

$online = $online1[1];
$status = $status1[1];

$output = '<div> Online Status: ' . $online . '</div><div> Status: ' . $status . '</div>';
echo $output;

11-05-2007, 06:12 PM
Give us an example of what this variable might be: $h3tag

So we can view the XML ourselves.

11-05-2007, 06:22 PM

Info2 will only have a variable if the user is signed on. The link above shows a user signed on.

Edit: Well now the user is offline. Normally it will show this:

<?xml version="1.0" ?>
- <XboxInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <PresenceInfo>
<Info>Playing Texas Hold'em</Info>
<Info2>Scenario - Single Player Playing a hand</Info2>
<Title>Texas Hold'em</Title>
<Gamertag>Babylons rise</Gamertag>
<ProfileUrl>http://live.xbox.com/member/Babylons rise</ProfileUrl>

11-05-2007, 08:31 PM
First of all, arrays start with item 0, not 1

$online = $online1[0];
$status = $status1[0];

I would think you would be looking here:

preg_match('#<StatusText>(.+?)</StatusText>#',$in1, $status1);

instead of here:

preg_match('#<Info2>(.+?)</Info2>#',$in1, $status1);

11-06-2007, 01:14 AM
No I want Info2 variable :)

And unfortunately setting those vars to 0 did nothing as well :(

11-06-2007, 04:16 AM
darn! double-foiled!

Maybe it has something to do with preg_match
when it should be preg_match_all?

11-06-2007, 08:56 AM
You really should use SimpleXML (http://pl2.php.net/manual/en/ref.simplexml.php)

11-06-2007, 11:26 AM
Yea unfortunately my host had php 4 installed and simplexml is php 5. I've requested the upgrade and I will reattempt the code using simplexml. However still doesn't explain why my code doesn't work ;)

11-06-2007, 01:37 PM
Enjoy the last 2 months of PHP4 support :)
PHP4 XML functions aren't that easy to use, but still a lot better than trying to use regex. http://www.php.net/manual/en/ref.xml.php