【问题标题】:PHP Undefined offset: 1PHP未定义偏移量:1
【发布时间】:2013-08-18 22:02:33
【问题描述】:

这是我的 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
";
}

?>

$npcs 变量返回错误Undefined offset: 1。我尝试了很多方法来解决它,但都没有奏效。另外,这里是 var_dump 的数据:

    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)

我正在尝试检索信息。另外,我可以检索服务器名称、位置和类似的东西吗?我不是很有经验。 :P

【问题讨论】:

  • $npcs = ''.$matches[1]; 如果这是返回错误的行,则表示上述preg_match() 可能找不到任何匹配项
  • $data 中没有“npcs”信息,因此与$npcs 一致的$matches 为空。
  • 而不是测试 $players/$monsters/etc。最好测试你的 preg_match。示例:if (preg_match(........)) { /*do something*/} else { /* do something else */}
  • @PawełPiecyk 谢谢,刚刚意识到。我怎样才能检索其他数据?

标签: php laravel preg-match laravel-4


【解决方案1】:

当您尝试使用不存在的索引访问数组项时,PHP 中会发生未定义的偏移错误,例如,假设我有以下内容:

<?php
$fruit = array('apple', 'orange', 'pear');
echo $fruit[0]; // apple
echo $fruit[1]; // orange
echo $fruit[3]; // Undefined offset:3 

在上面的示例中,数组包含三个项目。这些项目的索引是 0,1,2。没有索引为 3 的项目。因此,当我尝试访问 $fruit[3] 时出现错误。

在您的具体情况下,没有 $matches[1]。您应该对 preg_match() 调用进行故障排除,并查看您的模式是否返回任何匹配项。

作为更一般的建议,我建议您在尝试访问数组键之前检查它是否存在。

【讨论】:

    猜你喜欢
    • 2022-01-02
    • 2011-07-22
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    • 1970-01-01
    • 1970-01-01
    • 2020-01-22
    • 2014-10-01
    相关资源
    最近更新 更多