2

有一点编码问题,我如何检查 $row['value'] 的值是否包含某些字符,在这种情况下,如果“rename_file”包含一个包含“128”的文件名。我有这个,但它似乎没有回声。

$row = mysql_fetch_assoc($result);
{
while ($row = mysql_fetch_assoc($result))
  {
  echo $row['c_ID'] . " " . $row['source_file'] . " " . $row['rename_file'] ." " . $row['p_ID'];
  if ($row['rename_file'] = '%128%') {
  echo "<p> This is a 128";
  } else
  echo "<br>";
  }
}

非常感谢。CP

4

4 回答 4

2

使用preg_match()

if(preg_match('/128/',$row['rename_file'])){
    echo "<p> This is a 128";
} else {
    echo "<br>";
}

或者strpos()

if(strpos($row['rename_file'], '128') !== false){
    echo "<p> This is a 128";
} else {
    echo "<br>";
}
于 2013-07-17T17:19:20.697 回答
0

看看 stristr 搜索关键字。http://php.net/manual/en/function.stristr.php

于 2013-07-17T17:17:00.583 回答
0
if (strpos($row['rename_file'], '128') !== false) {
    echo "<p> This is a 128";
}
于 2013-07-17T17:17:48.390 回答
0

如果您要检查行中的每个值是否为 128:

function searchArray($search, $array)
{
    foreach($array as $key => $value)
    {
        if (stristr($value, $search))
        {
            return true;
        }
    }
    return false;
}

$row = array('c_ID'=>'Prefix_128_ABC','source_file'=>'EFG.xml','rename_file'=>'FOO.xml');
if (searchArray('128',$row) !== false) {
    echo "<p> This is a 128";
}else{
    echo "<p> This is not a 128";
}

稍微修改自: http ://forums.phpfreaks.com/topic/195499-partial-text-match-in-array/

——哎呀!误解。好吧,如果你也需要,这就是你会这样做的方式...... -

于 2013-07-17T17:46:04.123 回答