(PHP帮助)如果文件中存在特定编号,请执行此操作


(PHP help) If specific number exists in file, do this

这是一个与PHP相关的问题。

我已经广泛搜索了以下解决方案,但没有找到适合我的东西。如果有人可以帮助我,将不胜感激。

我的想法是创建一个登录页面,如果向用户提供/发送"特定"的预定注册号,则访问登录页面和文件中存在的编号,以执行/登录权限允许。

例如,如果一个用户的注册号

是 12345,而另一个用户的注册号是 123,则希望首先搜索该用户的号码是否存在,然后执行某些操作()。

因此,如果"123"已经存在,则搜索"12345"与"234"相同,因此它必须找到一个或多个特定的数字。

如果它有区别,那么在数字之前和/或之后都有一个"空格"。

这将在现有文件中。

法典:

<?php 
$filename = 'datafile.txt'; 
// Data file may contain the following number(s) or any other 
// 123 (no) 
// 1234 (match) 
// 12345 (no) 
// 123456 (no) 
// exact match needed to proceed to page fopen($filename); 
$searchfor = '1234'; 
// this needs to be a variable 
// I don't know what the number will be used. 
$file = file_get_contents($filename); 
if (preg_match($file, $searchfor)) {
  echo "Match found. Proceed to the page."; 
} else { 
  echo "Match not found. Try again."; 
} 
fclose($filename); 
?>

谢谢。任何帮助都是值得赞赏的,干杯。

佛瑞德

将它们

存储在平面文件中可能不是我这样做的方式,但如果我要使用平面文件,以下示例应该可以正常工作:

$nums = "12345 34 45 12345 23"; # use file_get_contents to load from file
$uNum = 34;
if ( preg_match( "/[^0-9]{$uNum}[^0-9]/", $nums ) ) {
  echo "Access";
} else {
  echo "Denied";
}

至于填充$nums字符串,您可以使用file_get_contents()来提取包含所有数字的文件内容。

/表示模式的开始[^0-9]   模式中的空间不能被任何数字占用{$uNum}  用户编号 - 上例中的 34[^0-9]   模式中的空间不能被任何数字占用/表示模式结束

从本质上讲,如果我们的数字在其两侧有一个数字而不是空格,或者根本没有,则找不到我们的数字。