【发布时间】:2012-06-13 22:15:49
【问题描述】:
目前,我有这个代码:
<?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!";
}
?>
非常感谢对我的问题有任何帮助!
【问题讨论】:
-
我想要一件蓝莓兔子服装
-
与问题无关的杂乱代码太多。尝试归结/编译一个合适的测试用例。
-
旁白:应该有
[scripting]标签的问题示例是什么?关于编写口译员的问题?当然,所有 ruby、python、php 等问题都不应该仅仅因为它是其中一种语言而用脚本自动标记。