dongzhi4239 2012-12-21 22:44
浏览 58
已采纳

在文本文件中搜索字符串

I get a user.txt file. There are all the users saved in this format:

Vorname: Michael | Nachname : Schmidt | Username: DarkDecipio | Passwort: 3609e297fb0b6fb1105cba71d5c024b3
Vorname: John | Nachname : Doe | Username: Dummy | Passwort: d78c03d72e72b44a131d255aec3c8a11

Now when a new User send a register form to the php file, i want to look, if the username is already taken. So i must search all Username's, if there the sended username. But i can't do it with strstr() or something like that, because the Prename (Vorname) or Surname (Nachname) can be the same like the Username. Or the username can include a part of the sended username...

Hope you understand it.
Note: I know it's a ugly solution to save users in a text file, but this is a project in my school and we MUST use textfiles...

  • 写回答

1条回答 默认 最新

  • douwei4370 2012-12-21 22:47
    关注

    Off the top of my head, I can think of two options. Assume $line is the current line in your file and you are looking for 'DarkDecipio':

    First, use regular expressions to ensure you are picking up the section you want:

    if ( preg_match('/Username:\s*DarkDecipio/', $line) ) {
        echo 'Match';
    }
    

    Second, split the lines by | and then use strstr on the third item in each array:

    $items = explode('|', $line);
    if ( strstr($items[2], 'DarkDecipio') ) {
        echo 'Match';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎么改成输入一个要删除的数后现实剩余的数再输入一个删除的数再现实剩余的数用yes表示继续no结束程序
  • ¥15 在启动roslaunch时出现如下问题
  • ¥15 汇编语言实现加减法计算器的功能
  • ¥20 关于多单片机模块化的一些问题
  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取
  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?