目前,我有以下代码:
<?php
if (isset($_GET['s'])) {
$itemid = $_GET['s'];
$search = "$itemid";
$query = ucwords($search);
echo "<title>Results for $query</title>";
$string = file_get_contents('http://clubpenguincheatsnow.com/tools/newitemdatabase/items.php');
if ($itemid == "") {
echo "Please fill out the form.";
} else {
echo '<div id="content">';
$string = explode('<br>', $string);
foreach ($string as $row) {
preg_match('/^(.+)'s='s('d+)'s='s('D+)'s='s('d+)/', trim($row), $matches);
if (preg_match("/$query/i", "$matches[1]")) {
echo "<a href='http://clubpenguincheatsnow.com/tools/newitemdatabase/info.php?id=$matches[2]'>";
echo $matches[1];
echo "</a><br>";
}
}
echo '</div>';
}
} else {
echo "Item does not exist!";
}
?>
如果"$matches[1]"中没有任何内容,我希望我的代码它回显"项目不存在!不过我该怎么做呢?请帮忙!
我以前尝试过类似if ($matches[1]=="") { echo "Item does not exist!"; }
的东西,但没有用。这就是我得到的:
http://img685.imageshack.us/img685/998/28990b2c12d0423292d3574.png
看到工作正常吧?看看如果$matches[1]确实存在会发生什么:
http://img528.imageshack.us/img528/3690/71472c9de6ec49118ee8d48.png
它仍然出现!如何制作我的代码,使其仅在没有$matches[1]的情况下回显错误?请帮帮我!
如果您想知道,这是我添加if ($matches[1]=="") { echo "Item does not exist!"; }
时的代码:
<?php
if (isset($_GET['s'])) {
$itemid = $_GET['s'];
$search = "$itemid";
$query = ucwords($search);
echo "<title>Results for $query</title>";
$string = file_get_contents('http://clubpenguincheatsnow.com/tools/newitemdatabase/items.php');
if ($itemid == "") {
echo "Please fill out the form.";
} else {
echo '<div id="content">';
$string = explode('<br>', $string);
foreach ($string as $row) {
preg_match('/^(.+)'s='s('d+)'s='s('D+)'s='s('d+)/', trim($row), $matches);
if (preg_match("/$query/i", "$matches[1]")) {
echo "<a href='http://clubpenguincheatsnow.com/tools/newitemdatabase/info.php?id=$matches[2]'>";
echo $matches[1];
echo "</a><br>";
}
}
echo '</div>';
if ($matches[1] == "") {
echo "Item does not exist!";
}
}
} else {
echo "Item does not exist!";
}
?>
对我的问题的任何帮助将不胜感激!
看看 PHP 的比较运算符
if (empty($matches[1])) { echo "Item does not exist!"; }
else {
echo "Item does not exist!";
}
这个"其他"是关于isset($_GET['s'])
.查询字符串中是否有名为 s
的变量?如果没有,那么您收到消息是正常的。
也许你把那个else
块放在了错误的地方?