如何查找$row是否包含某些字符


How to find if $row contains certain characters

有点编码问题,我如何检查$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

使用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>";
}

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

if (strpos($row['rename_file'], '128') !== false) {
    echo "<p> This is a 128";
}

如果您想检查行中的每个值是否为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/

--哎呀!误解好吧,如果你也需要的话,你会这样做