2014-04-21 110 views
1

字符串目前陣列ATA Report of 7th Apr 2014, ATA Report of 17th Apr 2014, ATA Report of 27th Apr 2014字符串與PHP中的子字符串比較?

我有子字符串中7th Apr 2014

我如何才能找到ATA Report of 7th Apr 2014位置

我甲肝像這樣使用,但不是爲我工作

$date = "7th Apr 2014" 
if (strpos("ATA Report of 7th Apr 2014", $date) !== false){ 
//my code......... 

} 
+0

你是什麼意思「沒爲我工作」它應該工作得很好 – samayo

+0

@Simon_eQ,它不會正常工作,因爲'strpos()'使用不當。 –

回答

1

您需要將它們循環。

<?php 
$arr=array("ATA Report of 7th Apr 2014", "ATA Report of 17th Apr 2014", "ATA Report of 27th Apr 2014"); 
foreach($arr as $k=>$v) 
{ 
    if(strpos($v,"7th Apr 2014") !== false) 
    { 
     echo "The position is $k"; 
     break; 
    } 
} 

此外,您正在以錯誤的方式使用它...乾草堆應該先來,然後是針。

+0

我想找到「2014年4月7日」第一場比賽發生的地方.......... – Lipsa

+0

@ user3518270,這就是代碼的作用。它返回位置0'2014年4月7日'是在數組的第一個位置。 –

+0

$ v =「2014年4月7日」;對 ? – Lipsa