doutan5798 2018-06-17 11:30 采纳率: 100%
浏览 51

在PHP数组中搜索以特定单词开头的值

I am loading a text file into an array:

<?php

$filename = "data.txt";

$array = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

?>

The file contains certain lines, that start with the word "Data". Those lines are loaded as separate values in the array.

What I wanna do is extract only those values from the array and save those as a separate file, each one on a new line.

How would I go about searching for those specific values in the array and doing the above?

  • 写回答

3条回答 默认 最新

  • doumibi6899 2018-06-17 11:48
    关注

    When you have lines of your file in an array, you are stuck with iterating through the array (lines) and checking whether the line starts with Data (0 === mb_strpos($line, 'Data')). If so, you just save the line to another file with fwrite().

    Or you could run grep in console:

    grep '^Data' input.txt > output.txt
    
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料