PHP Undefined offset: 1

Voici mon script PHP:

    <?php
$address = htmlentities(strtolower(Config::get('aac.ip')));
$port = Config::get('aac.port');
@$sock = fsockopen ($address, $port, $errno, $errstr, 1);
if(!$sock) echo '<small class="muted"><strong>Server status:</strong></small> <span class="label label-important">OFFLINE</span><br />'; else {
$info = chr(6).chr(0).chr(255).chr(255).'info';
fwrite($sock, $info);
$data='';
while (!feof($sock))$data .= fgets($sock, 1024);
fclose($sock);
//if needed
//var_dump($data);
preg_match('/players online="(\d+)" max="(\d+)"/', $data, $matches);
$players = ''.$matches[1].' /'.$matches[2];
preg_match('/uptime="(\d+)"/', $data, $matches);
$h = floor($matches[1] / 3600);
$m = floor(($matches[1] - $h*3600) / 60);
$uptime = ''.$h.'h '.$m.'m';
preg_match('/monsters total="(\d+)"/', $data, $matches); 
$monsters = ''.$matches[1]; 
preg_match('#<motd>(.*?)</motd>#s', $data, $matches); 
$motd = ''.$matches[1];
preg_match('/npcs total="(\d+)"/', $data, $matches);
$npcs = ''.$matches[1];
if(empty($players) or $players == " /") $players = "??? /???";
if(empty($uptime)) $uptime = "???";
if(empty($monsters)) $monsters = "???";
if(empty($motd)) $motd = "???"; 
echo "
<small class='muted'><strong>Server status:</strong></small> <span class='label label-success'>ONLINE</span><br />
<small class='muted'><strong>Players:</strong> $players</small><br />
<small class='muted'><strong>Uptime:</strong> $uptime</small><br />
<small class='muted'><strong>Monsters:</strong> $monsters</small><br />
<small class='muted'><strong>MOTD:</strong> $motd</small><br />
$npcs
";
}
?>

La $npcs variable renvoie une erreur Undefined offset: 1. J'ai essayé de résoudre de nombreuses façons, mais aucun d'entre eux travaillaient. Aussi, voici le var_dump de données:

    string '<?xml version="1.0"?>
<tsqp version="1.0"><serverinfo uptime="33360" ip="127.0.0.1" servername="Forgotten" port="7171" location="Europe" url="http://otland.net/" server="The Forgotten Server" version="0.2.15" client="9.86"/><owner name="" email="@otland.net"/><players online="2" max="1000" peak="2"/><monsters total="636"/><map name="forgotten" author="Komic" width="1000" height="1000"/><motd>Welcome to the Forgotten Server!</motd></tsqp>
' (length=442)

Je suis en train de récupérer l'info. Aussi, suis-je en mesure de récupérer le nom du serveur, l'emplacement, et des choses comme ça? Je ne suis pas connu beaucoup. 😛

  • $npcs = ''.$matches[1]; si c'est la ligne de retour de l'erreur, cela signifie que le ci-dessus preg_match() est probablement pas trouver toutes les correspondances
  • Il n'y a pas de 'pnj' information dans $data donc $matches en ligne avec $npcs est vide.
  • Au lieu de tester $joueurs/$monstres/etc. il est préférable de tester votre preg_match. Exemple: if (preg_match(........)) { /*do something*/} else { /* do something else */}
  • merci, vient de réaliser. comment pourrais-je récupérer d'autres données?
InformationsquelleAutor erm_durr | 2013-08-18