2012-04-25 27 views
0

了我想比较POST方法在一个.txt文件字符串有一个字符串...如果有匹配做动作......我得到了这个,但它似乎无法通过循环...它搜索匹配的电子邮件和.txt中的每三个字符串是一个电子邮件字符串,这就是为什么迭代三...

<?php 

$email = $_POST['user']; 
$password = $_POST['pass']; 
$filename = 'C:\xampp\htdocs\www\zavrsni\emailList.txt'; 

if (($row = file_get_contents($filename)) != '') { 
    $wordsArray = explode(' ', $row); 
    for ($i=0; $i<sizeof($wordsArray); $i+3) { 
     if (strcmp($wordsArray[$i], $email) == 0){ 
      //some action 
      exit(); 
     } 
    } 
} 
?> 
+0

只是使用'in_array' http://php.net/manual/en/function.in-array.php – mgraph 2012-04-25 17:35:52

+3

可能你的意思是'$ i + = 3'而不是'$ i + 3'。 – anubhava 2012-04-25 17:37:08

+0

就是这样......谢谢你anubhava – ljencina77 2012-04-25 17:51:48

回答

0
<?php 

/* content of emailList.txt 
user_1 password_1 [email protected] 
user_2 password_2 [email protected] 
user_3 password_3 [email protected] 
user_4 password_4 [email protected] 
user_999 password_999 [email protected] 
user_5 password_5 [email protected] 
*/ 

// set POST for testing only!! 
$_POST['user'] = '[email protected]'; 
$_POST['pass'] = 'test'; 
// 
$email = $_POST['user']; 
$email = strtolower($email); 
$password = $_POST['pass']; 
$filename = './emailList.txt'; 

// read entire file into an array, skipping empty lines and not adding return characters 
$trimmed_file_array = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); 

foreach ($trimmed_file_array AS $row) { 
    //print $row.'<br>'; 
    list($f_user, $f_password, $f_email) = explode(' ', $row); 
    $f_email = strtolower($f_email); 
    if ($email === $f_email) { 
    print 'found user: '.$f_user.' - password: '.$f_password.' - email: '.$f_email.'<br>'; 
    break; 
    } 
} 

?>