duanqu9292 2017-04-26 19:52
浏览 46
已采纳

忽略文本文件中每行的最后一个单词?

So I have a text file that has a persons name, email message and email address on each line. I need a way to output only the name and the email message, while just ignoring the the email address on the end.

My text file looks like this.

Joe Hello how are you? joe@yahoo.com
Jim Whats up? jim@yahoo.com
Megan How is everyone doing? megan@yahoo.com

I was using a foreach loop to print out each line, but it of course keeps the email address, so how would I do this but not add the email address?

$messages = file('messages.txt');
foreach($messages as $message)
{
  echo $message;

}
  • 写回答

2条回答 默认 最新

  • dswfyq6201 2017-04-26 20:02
    关注

    As I said in comment - find position of a last space in a string and get a substring from beginning to a found position:

    $messages = file('messages.txt');
    foreach($messages as $message)
    {
        echo substr($message, 0, strrpos($message, ' ')) . '<br />';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?