我似乎无法弄清楚这一点。我有4个多重复选框并使用implode存储它们...然后尝试用爆炸来抓取它们进行比较。我需要显示表单并检查其值,因此我需要查看他们检查的内容并默认显示该框为管理员审核。似乎没有爆炸工作,因为它将字符串存储在索引0
存储到数据库:
$pulled = implode(",",$pulled);
从数据库中检索
<?php
$pulled = '{pulled}'; // (expression engine CMS field)
echo "before Explode: $pulled <br>";
// returns: before Explode: Tanker,End/Bottom Dump,Flatbed,Van
$pulled = explode(",",$pulled);
echo "after Explode: <br>";
var_dump($pulled);
// returns: after Explode:
array(1) { [0]=> string(8) "Tanker,End/Bottom Dump,Flatbed,Van" }
$pos = strpos($pulled[0], 'Tanker');
if ($pos === false) {
echo "<br><br>The string 'Tanker' was not found in the string '$pulled[0]'";
} else {
echo "<br>The string 'Tanker' was found in the string '$pulled[0]'";
}
答案 0 :(得分:0)
它应该是:
if ($pulled[0] != 'Tanker') {
echo "<br><br>The string 'Tanker' was not found in the string '$pulled[0]'";
} else {
echo "<br>The string 'Tanker' was found in the string '$pulled[0]'";
}
因为正如您在var_dump的结果中看到的那样,数组的元素0正是您要搜索的字符串。所以不需要做任何其他事情,只需比较它。如果这不起作用,那么您没有正确获取原始字符串。此代码适用于我的机器:
$pulled = 'Tanker,End/Bottom Dump,Flatbed,Van ';
$pulled = explode(",",$pulled);
if ($pulled[0] != 'Tanker') {
echo "<br><br>The string 'Tanker' was not found in the string '$pulled[0]'";
} else {
echo "<br>The string 'Tanker' was found in the string '$pulled[0]'";
}
答案 1 :(得分:0)
也许你打算在整个弦乐中寻找油轮?
$pos = strpos($pulled, 'Tanker');
if ($pos === false) {
echo "<br><br>The string 'Tanker' was not found in the string '$pulled'";
} else {
echo "<br>The string 'Tanker' was found in the string '$pulled'";
}