删除文件中的完全匹配 [英] Deleting exact match in file

查看:110
本文介绍了删除文件中的完全匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



场景:用户访问带有预先分配的注册号码的登录页面(在datafile.txt中),当在字段中输入数字时,脚本搜索文件,如果存在数字。



如果存在,则继续下一个步。当下一步完成后,我希望它自动删除原来使用的数字。



我相信搜索和替换是搜索确切匹配的途径。

我的数据文件类似于以下内容:(datafile.txt)

  0 
12
123
1234
12345
123456

12,如果搜索结果将不会与123相同。

A脚本,我很前几天给了它,这是为了完成另一项任务:

  $ numbers = file_get_contents(datafile。文本); 

$ uNumber = $ _POST ['uNum'];

if(@preg_match(/([^ 0-9] {$ uNumber} [^ 0-9])/,$ numbers)){

echo 数字匹配;

}

我为新任务所做的尝试是以下,但没有成功:

$ $ p $ code = $ data = file_get_contents('datafile.txt','w +');

$ uNumber = $ _POST ['uNum']; (@preg_match(/([^ 0-9] {$ uNumber} [^ 0-9])/,$ numbers)){

$ numbers = preg_replace('。$ _ Post [uNum]','',$ numbers);

echo已存在但将从文件中删除。;


} else {
echo如果不在文件中,则显示消息。;
}

任何帮助都将不胜感激。



  $ index = file_get_contents()解决方案

datafile.txt);
$ bodycont = str_replace(要替换的字符串,你想要的字符串是什么,$ index);
$ fh = fopen(datafile.txt,w);
fwrite($ fh,$ bodycont);
fclose($ fh);

OR:

  $ file = file(datafile.txt,FILE_IGNORE_NEW_LINES); 
foreach($ file as $ text){
$ test [] = $ text;
}

//获取数组的大小

$ arraysize = sizeof($ test);

//使用for循环来检查数组中是否存在该字符串,并将其替换为

($ x = 0; $ x <$ arraysize; $ x ++)
{
if($ test [$ x] ==$ uNumber)
{

}
else
{
$ newarray [] = $ test [$ x];
}
}
$ stringData = implode(\\\
,$ newarray);
$ fh = fopen(datafile.txt,w);
fwrite($ fh,$ stringData);
fclose($ fh);

这应该以某种方式工作。 :D

I have searched far and wide here but with no solution to my problem (php).

Scenario: User visits a login page with a pre-assigned registration number (in datafile.txt), when entering the number in the field, script searches file if number(s) exists.

If it does exist, goes on to the next step. When the next step has been completed, I would like it to automatically "delete" the number(s) originally used once done.

I believe a "search and replace" is the way to go, while searching for an "exact" match.

My data file would resemble the following: (datafile.txt)

0
12
123
1234
12345
123456

12, would not be the same as 123 if searched.

A script that I was so kindly given a few days ago is this to accomplish another task:

$numbers = file_get_contents("datafile.txt");

$uNumber = $_POST['uNum'];

if ( @preg_match( "/([^0-9]{$uNumber}[^0-9])/", $numbers ) ) {

echo "Numbers match";

}

What I tried to do for a new task, was the following but with no success:

$numbers = file_get_contents('datafile.txt', 'w+');

$uNumber = $_POST['uNum'];

if ( @preg_match( "/([^0-9]{$uNumber}[^0-9])/", $numbers ) ) {

$numbers = preg_replace('.$_Post["uNum"]','',$numbers);

echo "Existed but will be deleted from file. ";


} else {
echo "Message showed if not in file.";
}

Any help will be greatly appreciated.

解决方案

How about this one:

$index = file_get_contents("datafile.txt"); 
$bodycont = str_replace("string to be replaced","what you want that string to be",$index);
$fh = fopen("datafile.txt","w");
fwrite($fh,$bodycont);
fclose($fh); 

OR:

 $file = file("datafile.txt", FILE_IGNORE_NEW_LINES);
 foreach($file as $text) {
 $test[] = $text;
 }

//get the size of the array

$arraysize = sizeof($test);

//use for loop to check if that string is found in the array and replace it

for($x=0;$x<$arraysize;$x++)
    {
               if($test[$x] == "$uNumber")
                {

                }
                else
                {
                   $newarray[] = $test[$x];
                }
    }
 $stringData = implode("\n",$newarray );
 $fh = fopen("datafile.txt","w");
 fwrite($fh,$stringData );
 fclose($fh);

this should somehow work. :D

这篇关于删除文件中的完全匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆