2013-07-17 80 views
1

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

+0

结果集有多大?你也可以通过在你的数据库查询中添加一个WHERE子句来做到这一点。 – andrewsi

+0

@andrewsi因为他对不包含'128'的条目做了些什么,所以我不确定这会有助于 – StephenTG

+0

@StephenTG - 好点。我以错误的方式解析了这个问题! – andrewsi

回答

1

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

感谢队友,这么简单两种工作! –

+0

@ChrisP嘿,我很高兴它的工作:) – DarkAjax

0
if (strpos($row['rename_file'], '128') !== false) { 
    echo "<p> This is a 128"; 
} 
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/

--OOPS!误解。那么,如果你需要的话,你也可以这么做... -

相关问题