dongzan9069 2012-11-04 10:00
浏览 196
已采纳

从php中的列表中获取特定数据

I have a string like this:

 house1- a normal house 

 house2-an office  

 house3-a scholl 

There are about 2000 lines. I want to get only house1,house2,house3 etc. from that it and put it in another file. Like:

 house1

 house2 

 house3

I understand that the explode function works for separating strings, but how can I do this?

  • 写回答

2条回答 默认 最新

  • dongmu2026 2012-11-04 10:18
    关注
    $fContents = file( 'path/to/your/file' );
    foreach ( $fContents as $row )
        file_put_contents( 'path/to/other/file', explode( '-', $row )[0] , FILE_APPEND );
    

    Shitty but working example (assuming you got 5.4+).


    for PHP 5.3:

    $fContents = file( 'path/to/your/file' );
    foreach ( $fContents as $row )
    {
        $firstField = explode( '-', $row );
        file_put_contents( 'path/to/other/file', $firstField[0] , FILE_APPEND );
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部