0

我似乎无法弄清楚这一点。我有 4 个多复选框并使用 implode 存储它们......然后尝试用爆炸来抓取它们进行比较。我需要显示带有检查值的表单,因此我需要查看他们检查的内容并默认显示该框以供管理员查看。Explode 似乎没有工作,因为它将字符串存储在索引 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]'";
}
4

2 回答 2

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 正是您要搜索的字符串。所以不需要做任何其他事情,只是比较它。如果这不起作用,那么你没有得到原始字符串 cvorrectly。此代码适用于我的机器:

$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]'";
}
于 2012-12-14T13:14:47.670 回答
0

也许您打算在整个字符串中寻找 Tanker?

$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'";
}
于 2012-12-14T13:29:48.727 回答