douci2022 2015-05-25 12:50
浏览 41
已采纳

如何在php中使用preg_match_all函数从peragraph中获取新行?

I have a peragraph with some new lines :

 First line

Second line

Third line

 And this is the last line

I want to get the second line from the above peragraph.

So the result I want should be :

 "Second line"

I have tried the following script with preg_match_all() function but I don't know why it's not working.

 <?php
 $pera="First line

 Second line

 Third line

  And this is the last line";


 preg_match_all("#
+{2}.*+#",$pera,$results);
print_r($results);

Do you have any idea how to get the second line from the paragraph?

Any help is much appriciated.

Thanks!

  • 写回答

3条回答 默认 最新

  • douchao1879 2015-05-25 13:03
    关注

    Only for the purpose demonstrated, explode is really better for performance, but if you do want/have to use regex, don't use preg_match_all. That makes it global but you don't need that so go with preg_match. Then, change the pattern:

    
    {2}.*
    

    This will match the second line including leading newline character.

    https://regex101.com/r/jA3dL9/1

    If you want to match w/o the newline, use a capturing group:

    
    {2}(.*)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,