PHP REGEX preg_match_all every line after a particular line

匆匆过客 提交于 2019-12-20 03:15:31

问题


Here is the sample string with my regex and code:

$str = "Supp Fees:
----------
Oral Glucose
Glucagon
OXYGEN";

$ptn = "/----------(?:\r\n(.+))+/m";
preg_match_all($ptn,$str,$matches);

echo"<pre>";
print_r($matches);
echo"</pre>";

I'm trying to match every line after '----------' the pattern above only returns the first line (Oral Glucose). I can repeat the '\r\n(.+)' part and return another line but there is no telling how many lines there will be.

Thanks!


回答1:


You could do this without regex:

$data = substr($str, strpos($str, '----------') + 10);
$matches = explode("\r\n", $data);



回答2:


<?php

$str = "Supp Fees:
----------
Oral Glucose
Glucagon
OXYGEN";


$str = explode('----------', $str);
preg_match_all("/[^\r\n].*/", $str[1], $matches);

echo"<pre>";
print_r($matches);
echo"</pre>";

?>

?



来源:https://stackoverflow.com/questions/5545478/php-regex-preg-match-all-every-line-after-a-particular-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!